The original title link is here: https://leetcode.com/problems/palindrome-permutation/
Topic:
Given a string, determine if a permutation of the string could form a palindrome.
For example,
"code"
, False, "aab"
True, "carerac"
.
Exercises
See if you can pair it up.
Time Complexity:o (n). Space:o (n).
AC Java:
1 Public classSolution {2 Public BooleanCanpermutepalindrome (String s) {3 if(s = =NULL|| S.length () <= 1){4 return true;5 }6hashset<character> HS =NewHashset<character>();7 for(inti = 0; I<s.length (); i++){8 if(!Hs.contains (S.charat (i))) {9 Hs.add (S.charat (i));Ten}Else{ One Hs.remove (S.charat (i)); A } - } - returnHs.size () = = 1 | | Hs.size () = = 0; the } -}
can be used bitmap
1 Public classSolution {2 Public BooleanCanpermutepalindrome (String s) {3 if(s = =NULL|| S.length () <= 1){4 return true;5 }6 int[] Map =New int[256];7 for(inti = 0; I<s.length (); i++){8Map[s.charat (i)]++;9 }Ten intCount = 0; One for(inti = 0; i<256; i++){ A if(count = = 0 && map[i]%2 = = 1) {//the first occurrence of frequency is an odd char -count++; -}Else if(Map[i]% 2 = = 1) {//the second occurrence of frequency is an odd char the return false; - } - } - return true; + } -}
Keep up with palindrome permutation ii.
Leetcode palindrome permutation