<div>
Given a string s, return true if a permutation of the string could form a palindrome.
Example 1:
<pre>Input: s = "code" Output: false </pre>
Example 2:
<pre>Input: s = "aab" Output: true </pre>
Example 3:
<pre>Input: s = "carerac" Output: true </pre>
Constraints:
1 <= s.length <= 5000sconsists of only lowercase English letters.
</div>
题目大意:
字符串的任一全排列是否存在回文字符串
解题思路:
数学题,也就是统计字符频率,奇数频率的字符最多有1个
解题步骤:
N/A
注意事项:
- 统计字符频率,奇数频率的字符最多有1个
Python代码:
1
2
3def canPermutePalindrome(self, s: str) -> bool:
char_to_count = collections.Counter(s)
return False if len([count for count in char_to_count.values() if count % 2 == 1]) > 1 else True
算法分析:
时间复杂度为O(n),空间复杂度O(1)


