Given a string n
representing an integer, return the closest integer (not including itself), which is a palindrome. If there is a tie, return the smaller one.
The closest is defined as the absolute difference minimized between two integers.
Example 1:
Input: n = "123" Output: "121"
Example 2:
Input: n = "1" Output: "0" Explanation: 0 and 2 are the closest palindromes but we return the smallest which is 0.
Constraints:
1 <= n.length <= 18
n
consists of only digits.n
does not have leading zeros.n
is representing an integer in the range [1, 1018 - 1]
.This problem asks to find the closest palindrome to a given number represented as a string. The solution employs a clever approach to generate candidate palindromes and then efficiently determine the closest one.
Approach:
Candidate Generation: The core idea is to generate a small set of candidate palindromes that are likely to be the closest to the input number. This avoids an exhaustive search which would be computationally expensive. The candidates are generated as follows:
Numbers with same length as input: The algorithm considers numbers formed by taking the first half of the input number (rounded up) and then mirroring it to create a palindrome. It explores three variations: one less than this prefix, the prefix itself, and one more than the prefix.
Numbers with length one less and one more than the input: The algorithm also includes two additional candidates: a number consisting of (10^(n-1))-1
(all nines) and 10^n +1
(a one followed by n zeros followed by another one). These are the smallest and the largest palindromes close to the input's length.
Closest Palindrome Selection: Once the candidates are generated, the algorithm iterates through them and identifies the closest palindrome to the input number based on the absolute difference. If there's a tie in the absolute differences, it selects the smaller palindrome.
Time Complexity Analysis:
Candidate Generation: The generation of candidate palindromes takes O(log n) time, where n is the length of the input string. This is because the number of digits in the input number determines the number of candidates generated.
Closest Palindrome Selection: The selection of the closest palindrome involves iterating through a constant number of candidates (at most 5). Therefore, the time complexity is O(1).
Overall Time Complexity: O(log n)
Space Complexity Analysis:
The space complexity is O(1) because the number of candidates generated and stored is constant, regardless of the size of the input number.
Code Explanation (Python):
The Python code is structured as follows:
class Solution:
def nearestPalindromic(self, n: str) -> str:
x = int(n)
l = len(n)
res = {10 ** (l - 1) - 1, 10**l + 1} # Candidates with length one less and one greater
left = int(n[: (l + 1) >> 1]) # Extract the left half of the number
for i in range(left - 1, left + 2): # Explore 3 candidates: one less, the left half, one more
j = i if l % 2 == 0 else i // 10 # Adjust for even/odd length
while j: #Construct the palindrome by mirroring the left half
i = i * 10 + j % 10
j //= 10
res.add(i)
res.discard(x) #Remove the input number itself
ans = -1
for t in res: #Find the closest candidate palindrome
if (
ans == -1
or abs(t - x) < abs(ans - x)
or (abs(t - x) == abs(ans - x) and t < ans)
):
ans = t
return str(ans)
The other languages (Java, C++, Go, Javascript) follow a very similar logic, adapting the syntax and data structures to their respective languages. The core algorithm remains the same.