Palindrome Rearrange
January 16, 2023
Problem
Given a string, find out if its characters can be rearranged to form a palindrome.
Example
For inputString = "aabb", the output should be solution(inputString) = true.
We can rearrange "aabb" to make "abba", which is a palindrome.
Solution
The condition of string to become palindrome is all character count must be even and only allow maximum 1 odd character. For example:
aabb
->abba
orbaab
a
= 2b
= 2aaabb
->ababa
orbaaab
a
= 3b
= 2
- Using hash byte of int to store character frequency
- Loop
inputString
to counter total char
Init total odd frequency as zero.
Iterate hash charCount
to find out how many odd frequency.
Return true
if odd frequency is less than equals 0
- Using hash byte of int to store character frequency
- Loop
inputString
to counter total char
Init total odd frequency as zero.
Iterate hash charCount
to find out how many odd frequency.
Return true
if odd frequency is less than equals 0