Search⌘ K
AI Features

Solution: Next Palindrome Using Same Digits

Explore how to find the next larger palindrome from a palindrome numeric string by rearranging its digits. This lesson teaches using the two-pointer strategy to manipulate the left half and mirror it, enabling you to produce the smallest lexicographically larger palindrome or determine when none exists. Understand key steps like finding the next permutation of the left half, swapping, and reversing to solve this problem efficiently.

Statement

Given a numeric string, num_str, 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 num_str 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:

  • ...