Next Special Palindrome Number

You are given an integer n.

A number is called special if:

  • It is a palindrome.
  • Every digit k in the number appears exactly k times.

Return the smallest special number strictly greater than n.

Example 1
Inputn = 2
Output22
22 is the smallest special number greater than 2, as it is a palindrome and the digit 2 appears exactly 2 times.
Example 2
Inputn = 33
Output212
212 is the smallest special number greater than 33, as it is a palindrome and the digits 1 and 2 appear exactly 1 and 2 times respectively.

Constraints

  • 0 <= n <= 10^15

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate