Before attempting this problem, you should be comfortable with:
Hash Maps / Frequency Counting - Counting character occurrences efficiently
Palindrome Properties - Understanding that palindromes require at most one character with odd frequency
Set Data Structure - Using sets to track elements with odd occurrences
1. Brute Force
Intuition
A string can form a palindrome if at most one character has an odd count. In a palindrome, characters mirror around the center, so pairs must exist for every character except possibly one in the middle.
The brute force approach counts occurrences of each possible ASCII character by iterating through the string for each character code. If more than one character has an odd frequency, no palindrome permutation is possible.
Algorithm
For each ASCII character (0 to 127), count how many times it appears in the string.
Track how many characters have odd counts.
If we find more than one character with an odd count, return false early.
Return true if at most one character has an odd frequency.
publicclassSolution{publicboolCanPermutePalindrome(string s){int[] countArr =newint[128];foreach(char c in s){
countArr[c]++;}int count =0;for(int i =0; i <128&& count <=1; i++){
count += countArr[i]%2;}return count <=1;}}
funccanPermutePalindrome(s string)bool{
countArr :=make([]int,128)for_, c :=range s {
countArr[c]++}
count :=0for i :=0; i <128&& count <=1; i++{
count += countArr[i]%2}return count <=1}
class Solution {funcanPermutePalindrome(s: String): Boolean {val countArr =IntArray(128)for(c in s){
countArr[c.code]++}var count =0for(i in0 until 128){
count += countArr[i]%2if(count >1)returnfalse}returntrue}}
classSolution{funccanPermutePalindrome(_ s:String)->Bool{var countArr =[Int](repeating:0, count:128)for c in s.unicodeScalars {
countArr[Int(c.value)]+=1}var count =0for i in0..<128{
count += countArr[i]%2if count >1{returnfalse}}returntrue}}
Time & Space Complexity
Time complexity: O(n)
If we generalize the solution to handle any Unicode character (no hardcoding): O(k⋅n). O(n2) In the worst case, if all characters are unique (k=n)
Space complexity: O(1) If the we assume the input contains only ASCII characters.
O(1) If the implementation is modiifed to handle Unicode characters.
Where n is the size of the input string s and where k is the number of unique characters in s
Common Pitfalls
Forgetting That One Odd Count Is Allowed
A common mistake is checking if all character counts are even. For odd-length palindromes, exactly one character can have an odd count (the middle character). The condition should be oddCount <= 1, not oddCount == 0.
Confusing Palindrome With Palindrome Permutation
Some developers attempt to check if the string itself is a palindrome rather than if any permutation can form one. The problem asks about rearranging characters, so only character frequencies matter, not their positions in the original string.
2. Using HashMap
Intuition
Instead of iterating through all possible ASCII characters, we can use a hash map to only track characters that actually appear in the string. This is more efficient when the string uses a small subset of the character set.
We count the frequency of each character, then check how many have odd counts. The palindrome condition remains the same: at most one odd frequency.
Algorithm
Use a hash map to store the frequency of each character in the string.
Iterate through the string once, incrementing counts for each character.
classSolution{/**
* @param {string} s
* @return {boolean}
*/canPermutePalindrome(s){const map =newMap();for(let i =0; i < s.length; i++){
map.set(s.charAt(i),(map.get(s.charAt(i))||0)+1);}let count =0;for(let key of map.keys()){
count += map.get(key)%2;}return count <=1;}}
publicclassSolution{publicboolCanPermutePalindrome(string s){Dictionary<char,int> map =newDictionary<char,int>();foreach(char c in s){if(!map.ContainsKey(c)) map[c]=0;
map[c]++;}int count =0;foreach(var kvp in map){
count += kvp.Value %2;}return count <=1;}}
funccanPermutePalindrome(s string)bool{
mp :=make(map[rune]int)for_, c :=range s {
mp[c]++}
count :=0for_, v :=range mp {
count += v %2}return count <=1}
class Solution {funcanPermutePalindrome(s: String): Boolean {val map = HashMap<Char, Int>()for(c in s){
map[c]= map.getOrDefault(c,0)+1}var count =0for((_, v)in map){
count += v %2}return count <=1}}
classSolution{funccanPermutePalindrome(_ s:String)->Bool{var map =[Character:Int]()for c in s {
map[c,default:0]+=1}var count =0for(_, v)in map {
count += v %2}return count <=1}}
Time & Space Complexity
Time complexity: O(n)
O(n+k) If we generalize the solution to handle any Unicode character. In the worst case (k=n), this becomes O(n).
Space complexity: O(1) If the we assume the input contains only ASCII characters.
O(k) If the implementation is modiifed to handle Unicode characters, the space complexity would depend on the number of unique characters in the string. O(n) in the worst case (if all characters are unique).
Where n is the size of the input string s and where k is the number of unique characters in s
Common Pitfalls
Forgetting That One Odd Count Is Allowed
A common mistake is checking if all character counts are even. For odd-length palindromes, exactly one character can have an odd count (the middle character). The condition should be oddCount <= 1, not oddCount == 0.
Confusing Palindrome With Palindrome Permutation
Some developers attempt to check if the string itself is a palindrome rather than if any permutation can form one. The problem asks about rearranging characters, so only character frequencies matter, not their positions in the original string.
3. Using Array
Intuition
Since we know the input contains only ASCII characters (128 possible values), we can replace the hash map with a fixed-size array. Array access is faster than hash map lookups, and we avoid the overhead of hashing.
The logic is identical to the hash map approach, but we use the character's ASCII value as the array index.
Algorithm
Create an array of size 128 to store character frequencies.
Iterate through the string, using each character's ASCII value as an index.
Increment the count at each index.
Count how many array positions have odd values.
Return true if at most one position has an odd count.
publicclassSolution{publicboolCanPermutePalindrome(string s){int[] map =newint[128];foreach(char c in s){
map[c]++;}int count =0;for(int i =0; i <128&& count <=1; i++){
count += map[i]%2;}return count <=1;}}
funccanPermutePalindrome(s string)bool{
mp :=make([]int,128)for_, c :=range s {
mp[c]++}
count :=0for i :=0; i <128&& count <=1; i++{
count += mp[i]%2}return count <=1}
class Solution {funcanPermutePalindrome(s: String): Boolean {val map =IntArray(128)for(c in s){
map[c.code]++}var count =0for(i in0 until 128){
count += map[i]%2if(count >1)returnfalse}returntrue}}
classSolution{funccanPermutePalindrome(_ s:String)->Bool{var map =[Int](repeating:0, count:128)for c in s.unicodeScalars {
map[Int(c.value)]+=1}var count =0for i in0..<128{
count += map[i]%2if count >1{returnfalse}}returntrue}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1) If the we assume the input contains only ASCII characters.
O(k) If the implementation is modiifed to handle Unicode characters, the space complexity would depend on the number of unique characters in the string. O(n) in the worst case (if all characters are unique).
Where n is the size of the input string s and where k is the number of unique characters in s
Common Pitfalls
Forgetting That One Odd Count Is Allowed
A common mistake is checking if all character counts are even. For odd-length palindromes, exactly one character can have an odd count (the middle character). The condition should be oddCount <= 1, not oddCount == 0.
Confusing Palindrome With Palindrome Permutation
Some developers attempt to check if the string itself is a palindrome rather than if any permutation can form one. The problem asks about rearranging characters, so only character frequencies matter, not their positions in the original string.
4. Single Pass
Intuition
We can optimize further by tracking the odd count dynamically as we process each character. When a character's frequency becomes even, we decrement the odd count. When it becomes odd, we increment. This way, we know the final answer immediately after one pass without needing a second loop.
Algorithm
Create an array of size 128 for character frequencies and initialize an odd counter to 0.
For each character in the string:
Increment its count in the array.
If the new count is even, decrement the odd counter.
If the new count is odd, increment the odd counter.
Space complexity: O(1) If the we assume the input contains only ASCII characters.
O(k) If the implementation is modiifed to handle Unicode characters, the space complexity would depend on the number of unique characters in the string. O(n) in the worst case (if all characters are unique).
Where n is the size of the input string s and where k is the number of unique characters in s
Common Pitfalls
Forgetting That One Odd Count Is Allowed
A common mistake is checking if all character counts are even. For odd-length palindromes, exactly one character can have an odd count (the middle character). The condition should be oddCount <= 1, not oddCount == 0.
Confusing Palindrome With Palindrome Permutation
Some developers attempt to check if the string itself is a palindrome rather than if any permutation can form one. The problem asks about rearranging characters, so only character frequencies matter, not their positions in the original string.
5. Using Set
Intuition
A set provides an elegant way to track characters with odd frequencies. When we see a character for the first time, we add it to the set. When we see it again, we remove it. Characters that appear an even number of times cancel out and leave the set. Only characters with odd frequencies remain.
At the end, if the set has at most one element, a palindrome permutation is possible.
Algorithm
Initialize an empty set.
For each character in the string:
If the character is already in the set, remove it.
Otherwise, add it to the set.
Return true if the set contains at most one character.
classSolution:defcanPermutePalindrome(self, s:str)->bool:
chars =set()for c in s:if c in chars:
chars.remove(c)else:
chars.add(c)returnlen(chars)<=1
classSolution{publicbooleancanPermutePalindrome(String s){Set<Character> set =newHashSet<>();for(int i =0; i < s.length(); i++){if(!set.add(s.charAt(i))) set.remove(s.charAt(i));}return set.size()<=1;}}
classSolution{/**
* @param {string} s
* @return {boolean}
*/canPermutePalindrome(s){const set =newSet();for(let i =0; i < s.length; i++){if(set.has(s.charAt(i))){
set.delete(s.charAt(i));}else{
set.add(s.charAt(i));}}return set.size <=1;}}
publicclassSolution{publicboolCanPermutePalindrome(string s){HashSet<char>set=newHashSet<char>();foreach(char c in s){if(set.Contains(c)){set.Remove(c);}else{set.Add(c);}}returnset.Count <=1;}}
funccanPermutePalindrome(s string)bool{
set :=make(map[rune]bool)for_, c :=range s {if set[c]{delete(set, c)}else{
set[c]=true}}returnlen(set)<=1}
class Solution {funcanPermutePalindrome(s: String): Boolean {valset= HashSet<Char>()for(c in s){if(c inset){set.remove(c)}else{set.add(c)}}returnset.size <=1}}
classSolution{funccanPermutePalindrome(_ s:String)->Bool{var charSet =Set<Character>()for c in s {if charSet.contains(c){
charSet.remove(c)}else{
charSet.insert(c)}}return charSet.count <=1}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1) If the we assume the input contains only ASCII characters.
O(k) If the implementation is modiifed to handle Unicode characters, the space complexity would depend on the number of unique characters in the string. O(n) in the worst case (if all characters are unique)
Where n is the size of the input string s and where k is the number of unique characters in s
Common Pitfalls
Forgetting That One Odd Count Is Allowed
A common mistake is checking if all character counts are even. For odd-length palindromes, exactly one character can have an odd count (the middle character). The condition should be oddCount <= 1, not oddCount == 0.
Confusing Palindrome With Palindrome Permutation
Some developers attempt to check if the string itself is a palindrome rather than if any permutation can form one. The problem asks about rearranging characters, so only character frequencies matter, not their positions in the original string.