Problem
Given a strings, return true if a permutation of the string could form a palindrome and false otherwise.
Examples
Constraints
1 <= s.length <= 5000sconsists of only lowercase English letters.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 266 with 15 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 266 # by problem number
lcpy gen -s palindrome_permutation # by problem name
s, return true if a permutation of the string could form a palindrome and false otherwise.
Input: s = "code"
Output: false
Input: s = "aab"
Output: true
Input: s = "carerac"
Output: true
1 <= s.length <= 5000s consists of only lowercase English letters.from collections import Counter
class Solution:
# Time: O(n)
# Space: O(1) for the 26-letter alphabet
def can_permute_palindrome(self, s: str) -> bool:
counts = Counter(s)
return sum(count % 2 for count in counts.values()) < 2
| Time | Space |
|---|---|
| O(n) | O(1) for the 26-letter alphabet |