266. Palindrome Permutation
https://leetcode.com/problems/palindrome-permutation
- CTCI 1.4 Palindrome Permutation
Python
from collections import Counter
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
counter = Counter(s)
see_center = False
for count in counter.values():
if count %2 == 0:
continue
if see_center:
return False
see_center = True
return True
Rust
impl Solution {
pub fn can_permute_palindrome(s: String) -> bool {
let mut mapper = [0; 26];
for i in s.as_bytes().iter() {
mapper[(i-b'a') as usize] += 1
}
let mut see_center = false;
for i in 0..mapper.len() {
let count = mapper[i];
if count %2 == 0 {
continue;
}
if see_center {
return false;
}
see_center = true;
}
true
}
}