LeetCode 1400. Construct K Palindrome Strings
考点 | 难度 |
---|---|
Greedy | Easy |
题目
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.
思路
如果出现奇数次的数字个数小于k,return false。如果k大于s的长度,return false。
答案
class Solution(object):
def canConstruct(self, s, k):
return sum(v % 2 for v in collections.Counter(s).values()) <= k <= len(s)