Search⌘ K
AI Features

Solution: Next Palindrome Using Same Digits

Explore how to compute the next smallest palindrome larger than a given palindrome numeric string by rearranging its digits. Learn to apply the two pointers approach to generate the immediate next lexicographical permutation of the palindrome's left half, then mirror it to form the new palindrome. Understand the method to efficiently determine this next palindrome, including handling even and odd length cases.

Statement

Given a numeric string, numStr, representing a palindromeA palindrome is a number that reads the same backward as it does forward. (composed only of digits). Return the smallest palindrome larger than numStr that can be created by rearranging its digits. If no such palindrome exists, return an empty string "".

Consider the following example to understand the expected output for a given numeric string:

  • input string = "123321"

  • The valid palindromes made from the exact digits are "213312", "231132", "312213", "132231", "321123".

  • We return the palindrome "132231" because it is the smallest palindrome larger than the input string "123321".

Constraints:

  • ...