Palindrome Permutation
1.4 Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.
EXAMPLE
My first method involve using a hash table. While this algorithm will preform in O(N) time, we use a long of additional extra space.
I first store the elements into a hash table, along with their frequencies, making sure to ignore spaces, and invalid characters.
Next, I iterate through this hash table to check for valid frequencies. Even lengthed strings must have a frequencies of even elements, and odd length strings must have 1 single frequency char with the remainder of the characters having a even number of frequencies.
The second approach is more attractive in my opinion, although it is a little less elegent. The approach is to first sort a valid string, and then compare the elements in place to confirm whether the next element is a duplicate. I've applied the same idea for when an element is odd, we allow for one free be.
O(nlogn) time, O(1) space
Last updated