Leetcode Problem 1842. Next Palindrome Using Same Digits
1842. Next Palindrome Using Same Digits
Leetcode Solutions
Next Permutation Approach for Palindrome
Split the input string into two halves. If the length is odd, store the middle character separately.
Compute the next permutation of the first half.
Find the rightmost character that is smaller than the character to its right.
Find the smallest character on the right side of the found character that is larger than the found character.
Swap these two characters.
Reverse the substring to the right of the initially found character.
If the next permutation does not exist, return an empty string.
Form the new palindrome by concatenating the next permutation of the first half, the middle character (if the length is odd), and the reverse of the next permutation of the first half.