266 - Palindrome Permutation
Written on October 30, 2015
Tweet
Given a string, determine if a permutation of the string could form a palindrome.
public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null || s.length() == 0) return false;
int[] count = new int[256];
for (char c : s.toCharArray()) {
if (count[c] > 0) {
count[c] --;
} else {
count[c] ++;
}
}
int single = 0;
for (int n : count) {
if (n != 0) {
single ++;
}
if (single > 1) {
return false;
}
}
return true;
}
}
class Solution(object):
def canPermutePalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
if not s:
return True
char_count = collections.Counter(s)
found_single = False
for count in char_count.values():
if count % 2 != 0:
if found_single:
return False
found_single = True
return True