Next Special Palindrome Number
A number is called special when both of these conditions hold:
- It reads the same forwards and backwards.
- Every digit
dthat appears in the number appears exactlydtimes.
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
Input: 6
Output: 22
Hints
A palindrome has at most one digit with an odd count, and that digit must sit in the center.
Since digit `d` contributes exactly `d` copies, enumerate the subsets of `1` through `9` and keep the subsets whose total length has the right parity.
Build a palindrome from its left half plus the optional center, mirror the half, and choose the smallest generated value above `n`.
Related Problems
Next Special Palindrome Number
A number is called special when both of these conditions hold: