Given a string s
, return true
if a permutation of the string could form a
palindrome and false
otherwise.
Example One:
Input: s = "code"
Output: false
Example Two:
Input: s = "aab"
Output: true
Example Three:
Input: s = "carerac"
Output: true
Solutions:
class Solution {
public boolean canPermutePalindrome(String s) {
Set < Character > set = new HashSet < > ();
for (int i = 0; i < s.length(); i++) {
if (!set.add(s.charAt(i)))
set.remove(s.charAt(i));
}
return set.size() <= 1;
}
}
Hints:
Logics:
- use hashSet to excude the repeat char
- if the set didn't add the char then remove the same character
- if there is one common or no character return true
- if there is more than two characters, return false
Tips:
-
if (!set.add(s.charAt(i)))
set.remove(s.charAt(i))
-
know how to use the methods