LeetCode-Solutions icon indicating copy to clipboard operation
LeetCode-Solutions copied to clipboard

Adding solve for 1400

Open NirBinyamin8 opened this issue 1 year ago • 3 comments

Given a string s and an integer k, return true if you can use all the characters in s to construct k palindrome strings or false otherwise.

The Readme.md file has been updated.

NirBinyamin8 avatar May 03 '24 10:05 NirBinyamin8

I can tell this is your first pull request! Thank you I'm so honored. :tada::tada::tada: I'll take a look at it ASAP!

welcome[bot] avatar May 03 '24 10:05 welcome[bot]

issue #484

NirBinyamin8 avatar May 03 '24 10:05 NirBinyamin8

from collections import Counter

def can_construct_palindrome(s, k):
    # Count the frequency of each character in the string
    char_count = Counter(s)
    
    # Count the number of characters with odd frequency
    odd_count = sum(count % 2 for count in char_count.values())
    
    # If k is greater than or equal to the number of odd-count characters,
    # it's possible to construct k palindromic strings
    return k >= odd_count and k <= len(s)

bitsgorilla avatar Jun 12 '24 10:06 bitsgorilla