Tap here to switch tabs
Problem
Submissions
Solution

Solution: Palindrome Permutation

Statement

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

Tap here to switch tabs
Problem
Submissions
Solution

Solution: Palindrome Permutation

Statement

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