Find the Closest Palindrome - Complete Solution Guide
Find the Closest Palindrome is LeetCode problem 564, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem requires finding the closest palindromic integer to a given integer (represented as a string). The closest integer shouldn't be the number itself, and if there's a tie (equal absolute differences), the smaller palindrome should be returned. The input 'n' is a string representing an integer between 1 and 10^18 - 1, with no leading zeros.
Solution Approach
The solution generates a set of candidate palindromes based on the input number. These candidates include palindromes formed by decrementing, keeping, and incrementing the first half of the input number. It also includes palindromes that are one less than the smallest number with the same number of digits, and one more than the largest number with the same number of digits, handling edge cases around powers of 10. The code then iterates through the candidates, computes the absolute difference between each candidate and the input number, and selects the candidate with the smallest difference. If there's a tie in the difference, it selects the smaller palindrome. The result is returned as a string.
Step-by-Step Algorithm
- Step 1: Determine the length of the input string 'n'.
- Step 2: Convert the input string 'n' to an integer (or long).
- Step 3: Create a set (or array in C) of candidate palindromes. Add two initial candidates: 10^(length - 1) - 1 and 10^length + 1. These cover the cases where the closest palindrome has fewer or more digits.
- Step 4: Extract the prefix of 'n', which is the first (length + 1) // 2 digits.
- Step 5: Generate palindromes by modifying the prefix. Iterate through prefix - 1, prefix, and prefix + 1. For each modified prefix, create a palindrome by mirroring it. If length is odd, exclude the last digit of the mirrored part.
- Step 6: Convert generated palindromes from Step 5 into integers (or longs) and add them to the set of candidates.
- Step 7: Remove the original number 'n' from the set of candidates to ensure we're returning a different palindrome.
- Step 8: Iterate through the candidates, calculate the absolute difference between each candidate and 'n'.
- Step 9: Keep track of the minimum difference found so far and the corresponding candidate.
- Step 10: If a candidate has a smaller difference, update the minimum difference and the result. If the difference is equal to the minimum difference, update the result with the smaller candidate.
- Step 11: Convert the final result back to a string and return it.
Key Insights
- Insight 1: Palindromes are symmetric, so the first half determines the second half. We can generate candidate palindromes by manipulating the first half of the input number.
- Insight 2: Consider edge cases like numbers close to powers of 10 (e.g., 100, 99) and extremely large numbers to ensure the generated palindromes are within a reasonable range and valid.
- Insight 3: Need to efficiently compare distances and handle ties, favoring the smaller palindrome in case of equal distance.
Complexity Analysis
Time Complexity: O(log n)
Space Complexity: O(1)
Topics
This problem involves: Math, String.
Companies
Asked at: Yelp.