...

/

Solution: Palindrome Permutation

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 ...

Access this course and 1400+ top-rated courses and projects.