Next Special Palindrome Number

A number is called special when both of these conditions hold:

  • It reads the same forwards and backwards.
  • Every digit d that appears in the number appears exactly d times.

Given an integer n, return the smallest special number that is strictly greater than n.

Because every present digit d contributes exactly d copies, a special number uses a subset of the digits 1 through 9, and digit 0 never appears. The total digit count is the sum of that subset, so the search space is small even though the answer can be large.

Examples
Input: 1
Output: 22
Hints

Next Special Palindrome Number

A number is called special when both of these conditions hold: