Given a string, determine if a permutation of the string could form a palindrome.
Example 1:
Input: "code"
Output: false
Example 2:
Input: "aab"
Output: true
Example 3:
Input: "carerac"
Output: true
------------------
2 pass.
可以用set来实现single pass,加加减减
class Solution { public boolean canPermutePalindrome(String s) { Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); map.put(c, map.getOrDefault(c, 0) + 1); } boolean odd = false; for (Map.Entry<Character, Integer> entry : map.entrySet()) { if (entry.getValue() % 2 == 1) { if (odd) return false; odd = true; } } return true; } }
No comments:
Post a Comment