LeetCode-Python-1400. Construct K palindrome strings (string + hash table)

Gives you a string s and an integer k. Please use all the characters in the s string to construct k non-empty palindrome strings.

If you can construct k palindrome strings with all characters in s, then please return True, otherwise return False.

 

Example 1:

Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct 2 palindrome strings with all characters in s.
Some possible construction schemes include: "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:

Input: s = "leetcode", k = 3
Output: false
Explanation: Cannot construct 3 palindrome strings with all characters in s.
Example 3:

Input: s = "true", k = 4
Output: true
Explanation: The only feasible solution is to make each character in s form a string separately.
Example 4:

Input: s = "yzyzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: You only need to put all z in one string and all y in another string. Then both strings are palindrome strings.
Example 5:

Input: s = "cr", k = 7
Output: false
Explanation: We do not have enough characters to construct 7 palindrome strings.
 

prompt:

1 <= s.length <= 10 ^ 5
All characters in s are lowercase English letters.
1 <= k <= 10 ^ 5

Source: LeetCode (LeetCode)
link: https://leetcode-cn.com/problems/construct-k-palindrome-strings
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

Ideas:

First deal with the simpler case:

1. If the length of s is equal to k, then each character forms a string separately, and the answer must exist.

2. If the length of s is smaller than k, then the answer must not exist.

 

Now we can guarantee that the length of s is greater than k.

Second, count the number of occurrences of each element in s:

1. If the number of occurrences of an element is even (k), we can use at least zero palindrome strings and at most k palindrome strings.

2. If the number of occurrences of an element is odd (k), then in order to process this element, we must at least need a palindrome string, and at most k palindrome strings can be used.

So what actually affects the answer is the number of elements that appear odd times. Assuming that the number of occurrences of A elements is odd, then we need at least A palindrome strings.

For other elements with an even number of occurrences, we can wrap the even number of characters to the existing palindrome string, so it has no effect on the number of palindrome strings.

Time complexity: O (N)

Space complexity: O (N)

class Solution(object):
    def canConstruct(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: bool
        """
        if k >= len(s):
            # 长度 == k,必然可以;长度 < k,必然不可以。
            return k == len(s)

        from collections import Counter
        dic = Counter(s) 

        s_odd = 0
        for val in dic.values():
            if val % 2:
                s_odd += 1
        return s_odd <= k

 

Published 734 original articles · 121 praises · 210,000 views

Guess you like

Origin blog.csdn.net/qq_32424059/article/details/105549613