564. Find the Closest Palindrome
Difficulty:Hard
Topics:Math, String
Given a string n representing an integer, return _the closest integer (not including itself), which is a palindrome-. If there is a tie, returnthe 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].
Hint:
- Will brute force work for this problem? Think of something else.
- Take some examples like 1234, 999,1000, etc and check their closest palindromes. How many different cases are possible?
- Do we have to consider only left half or right half of the string or both?
- Try to find the closest palindrome of these numbers- 12932, 99800, 12120. Did you observe something?
Solution:
We'll focus on creating a function that generates potential palindrome candidates and then selects the one closest to the input number.
Solution Approach:
Identify Palindrome Candidates:
- Mirror the first half of the number to form a palindrome.
- Consider edge cases like all digits being 9, 100...001, or 99...99.
- Generate palindromes by modifying the middle of the number up or down by 1.
Calculate the Closest Palindrome:
- For each palindrome candidate, compute the absolute difference with the original number.
- Return the palindrome with the smallest difference. If there's a tie, return the smaller palindrome.
Let's implement this solution in PHP:564. Find the Closest Palindrome
Explanation:
- generatePalindrome($firstHalf, $isOddLength):
- This helper function creates a palindrome by mirroring the first half of the number.
Copy after login
Edge Cases:
- Palindromes generated from numbers like 100...001 or 99...99 are handled by explicitly checking these cases.
Main Logic:
- We calculate possible palindromes and then find the closest one by comparing absolute differences.
This solution efficiently narrows down possible palindrome candidates and picks the closest one by considering only a few options, making it much faster than brute-force approaches.
Contact Links
If you found this series helpful, please consider giving therepositorya star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of . Find the Closest Palindrome. For more information, please follow other related articles on the PHP Chinese website!