Tap here to switch tabs
Problem
Submissions

Problem: Next Palindrome Using Same Digits

hard
40 min
Explore how to solve the problem of finding the next smallest palindrome larger than a given numeric palindrome by rearranging its digits. This lesson teaches you to apply two-pointer techniques and understand constraints to implement an efficient solution, preparing you for similar coding interview challenges.

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:

  • 11 \leq num_str.length 105\leq 10^5

  • num_str is a palindrome.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: Next Palindrome Using Same Digits

hard
40 min
Explore how to solve the problem of finding the next smallest palindrome larger than a given numeric palindrome by rearranging its digits. This lesson teaches you to apply two-pointer techniques and understand constraints to implement an efficient solution, preparing you for similar coding interview challenges.

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:

  • 11 \leq num_str.length 105\leq 10^5

  • num_str is a palindrome.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths