Find Next Palindrome Number
StringsAuto-graded · 29 test casesLast updated
Tests string manipulation and numeric logic to determine if a number is palindromic and compute the next larger palindrome efficiently.
You are given a string num representing a positive integer. Your task is to:
- Determine whether
numrepresents a palindromic number - Find the smallest palindromic number that is strictly larger than
num
Return a list containing two elements: a boolean indicating whether the input is a palindrome, followed by a string representing the next larger palindrome.
Notes
- The input is always a valid positive integer with no leading zeros
- The result may be too large for standard integer types, so work with strings throughout
- When the input is already palindromic, you must find the next palindrome after it, not return the input itself
Constraints
1 <= len(num) <= 10^5numconsists only of digits'0'through'9'numdoes not have leading zeros
Examples
Example 1
Input: find_next_palindrome('121')
Output: [True, '131']
Example 2
Input: find_next_palindrome('999')
Output: [True, '1001']
Example 3
Input: find_next_palindrome('12345')
Output: [False, '12421']
Example 4
Input: find_next_palindrome('1221')
Output: [True, '1331']
Hints
Hint 1
A palindrome is determined entirely by its first half—the second half is just a mirror
Hint 2
When all digits are 9, the next palindrome must have one more digit
Hint 3
Try mirroring the left half to the right; if that's not large enough, increment the middle portion and mirror again
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that a palindrome of a given length is completely determined by its first half—the second half must mirror it. To find the next palindrome efficiently:
-
Check if the input is palindromic by comparing it to its reverse.
-
Handle the special case of all 9s: When the input consists entirely of
9s (like"999"), no palindrome of the same length can be larger. The answer is"1" + "0"*(n-1) + "1", which jumps to the next length (e.g.,"999"→"1001"). -
Try mirroring the left half: Copy the first half of digits to the second half to create a palindrome. If this mirrored result is strictly greater than the input, we're done.
-
Increment the middle and mirror: If mirroring alone doesn't produce a larger number, we need to increment the center portion. Find the middle digit(s) and add 1, handling carries as needed. Since we've already ruled out the all-9s case, this increment won't overflow to a longer number. After incrementing, mirror this new left half to get the final result.
Why It Works
Palindromes of length n are ordered exactly as their left halves are ordered. The smallest palindrome with a given left half is obtained by mirroring that half. If we can't beat the input by mirroring its own left half, we need the next left half in sequence—which is the current left half plus one.
The algorithm never needs to convert the entire number to an integer or loop through candidates, making it efficient even for very long digit strings.
Complexity
- Time: O(n) where n is the length of the input. We perform a constant number of linear scans: checking palindrome, mirroring, comparing, and possibly incrementing.
- Space: O(n) for storing the candidate digits and output string.
Solution
def find_next_palindrome(num: str) -> List:
is_palindrome = num == num[::-1]
n = len(num)
if all(ch == '9' for ch in num):
return [is_palindrome, '1' + '0' * (n - 1) + '1']
candidate = list(num)
for i in range(n // 2):
candidate[n - 1 - i] = candidate[i]
palindrome = ''.join(candidate)
if palindrome <= num:
left = (n - 1) // 2
right = n // 2
while left >= 0 and candidate[left] == '9':
candidate[left] = '0'
candidate[right] = '0'
left -= 1
right += 1
candidate[left] = str(int(candidate[left]) + 1)
candidate[right] = candidate[left]
return [is_palindrome, ''.join(candidate)]