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
Constraints:
-
st.length
- 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 is , and it requires iterating through the ...