Given a string, determine if a permutation of the string could form a palindrome.
For example,
"code" -> False, "aab" -> True, "carerac" -> True.
判断一个字符串的字母排列之后是否能组成回文字符串
很快就做出来了 思路是
回文字符串中的字符都是成对的 最多有一个单独的
但是solution给出了很多实现方式 下面的single pass写的不错
public class Solution {
public boolean canPermutePalindrome(String s) {
int[] map = new int[128];
int count = 0;
for (int i = 0; i < s.length(); i++) {
map[s.charAt(i)]++;
if (map[s.charAt(i)] % 2 == 0)
count--;
else
count++;
}
return count <= 1;
}
}
本文介绍了一种快速判断给定字符串是否可以通过重新排列其字符形成回文串的方法。核心思路在于回文串中字符出现次数的特点:除了可能有一个字符出现奇数次外,其余字符均需成对出现。
481

被折叠的 条评论
为什么被折叠?



