266. Palindrome Permutation - cocoder39/coco39_LC GitHub Wiki
notes 2024
2-pass
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
counter = [0] * 26
for ch in s:
counter[ord(ch) - ord('a')] += 1
odd = 0
for count in counter:
if count % 2:
odd += 1
if odd > 1:
return False
return True
1-pass
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
counter = [0] * 26
odd = 0
for ch in s:
index = ord(ch) - ord('a')
counter[index] += 1
if counter[index] % 2 == 0:
odd -= 1
else:
odd += 1
return odd <= 1
================================
bool canPermutePalindrome(string s) {
vector<int> count(256);
int len = s.length();
for(int i = 0; i < len; i++){
count[s[i]]++;
}
int odd = 0;
for(int i = 0; i < count.size(); i++){
if(count[i] % 2){// odd
odd++;
if(odd > 1){
return false;
}
}
}
return true;
}