661. Next Palindrome Using Same Digits

Given a numeric string num, which represents a very large palindrome. The task is to find the smallest palindrome larger than num that can be formed by rearranging its digits. If no such palindrome exists, return an empty string "".

A palindrome is a number that reads the same forward and backward.

Example 1:

Input: num = "1331"

Output: "3113"

Explanation:

The next palindrome larger than "1331" that can be formed by rearranging its digits is "3113".

Example 2:

Input: num = "43234"

Output: ""

Explanation:

No palindrome larger than "43234" can be formed using its digits.

Now Your Turn!

Pick the correct output for the given input

Input: num = "566655"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ num.length ≤ 105
  • num is a palindrome.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
string nextPalindrome(string num) {
}
};
 
Test Case

Input:

Num