Solution: Palindrome Permutation

Let's solve the Palindrome Permutation problem using the Knowing What to Track pattern.

Statement

For a given string, st, find whether or not a permutation of this string is a palindromePalindrome is a string of characters that reads the same forwards and backwards. For example, “ababa” and “xyxxyx” are palindromes.. You should return TRUE if such a permutation is possible and FALSE if it isn’t possible.

Constraints:

  • 11 \leq st.length 1000\leq 1000
  • The string will contain lowercase English letters.

Solution

So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.

Naive approach

The naive solution is to first compute all possible permutations of a given string and then iterate over each permutation to see if it’s a palindrome. We can use two pointers to traverse the computed permutation from the beginning and the end simultaneously, comparing each character at every step. If the two pointers point to the same character until they cross each other, the string is a palindrome.

The number of possible permutations for a string of length nn is n!n!, and it requires iterating through the nn characters to construct each permutation. Therefore, it will take O(n!×n)O(n! \times n) time to generate all these permutations. Then, for each permutation, we need to check whether it is a palindrome. Checking this condition requires iterating through the permutation once, which takes O(n)O(n) time. Therefore, the overall time complexity of this naive approach is O((n!×n)×n)O((n! \times n)\times n). The space complexity is O(n!)O(n!) because we need to store all the permutations.

Optimized approach using frequency counting

This is the stereotypical example of a problem whose naive solution is very inefficient, yet by identifying a few key properties of the problem, we are able to devise a solution with superior time and space complexity measures. In this case, there are three key observations:

  • In a palindrome of even length, all the characters occur an even number of times. For example, in the palindrome “aaaabbaaaa”, “a” occurs four times and “b” occurs twice.
  • In a palindrome of odd length, the character in the middle of the string appears once, and all the others occur an even number of times. For example, in the palindrome “aaabaaa”, “a” occurs six times and “b” occurs once.
  • The observations above are true for all the permutations of a palindrome. For example, “aaaaaaaabb” is a permutation of “aaaabbaaaa” in which “a” still occurs four times and “b” occurs twice.

So, to decide whether or not a given string has a permutation that is a palindrome, all we need to do is count the number of times each character appears in it and then check how many characters appear an odd number of times.

  • If no characters appear an odd number of times, then the string is of even length and has a permutation that is a palindrome.
  • If only one character appears an odd number of times, then the string is of odd length and has a permutation that is a palindrome.
  • If more than one character appears an odd number of times, then the string does not have a permutation that is a palindrome.

The slides below illustrate the working of our proposed solution:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.