-
-
Notifications
You must be signed in to change notification settings - Fork 155
Find the Closest Palindrome (Leetcode) Added problem number 564 #1021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
dsa-solutions/lc-solutions/0500-0599/0564- Find the Closest Palindrome.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
--- | ||
id: find-the-closest-palindrome | ||
title: Find the Closest Palindrome | ||
sidebar_label: 564 Find the Closest Palindrome | ||
tags: | ||
- Java | ||
- string | ||
- Math | ||
description: "This document provides a solution where we need to find the nearest palindrome to a given number n, the idea is to generate potential palindromic candidates close to n and then determine which one is closest in terms of absolute difference. " | ||
--- | ||
|
||
## Problem | ||
|
||
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. | ||
|
||
### Examples | ||
|
||
**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, 10^18 - 1]. | ||
ajay-dhangar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
--- | ||
|
||
## Approach | ||
|
||
To solve the problem, we need to understand the nature of the allowed moves: | ||
|
||
1. **Generate Candidate Palindromes**: | ||
|
||
- Generate palindromes by reflecting the first half of the number. | ||
|
||
- Create palindromes by incrementing or decrementing the first half of the number. | ||
|
||
- Consider edge cases such as numbers with all $9's$ or all $0's$. | ||
|
||
2. **Calculate Distances**: | ||
|
||
- For each candidate palindrome, compute the absolute difference from the original number $n$. | ||
|
||
3. **Select the Closest Palindrome**: | ||
|
||
- Among all candidates, select the one with the smallest absolute difference. In case of ties, choose the smaller number. | ||
|
||
## Solution for Finding the Closest Palindrome | ||
|
||
The given problem involves To find the nearest palindrome to a given number 'n', the idea is to generate potential palindromic candidates close to 'n' and then determine which one is closest in terms of absolute difference. | ||
|
||
#### Code in Java | ||
|
||
```java | ||
class Solution { | ||
public String nearestPalindromic(String n) { | ||
long num = Long.parseLong(n); | ||
int len = n.length(); | ||
|
||
// Edge cases for 1, 0, 10, 100, etc. | ||
long smaller = (long) Math.pow(10, len - 1) - 1; | ||
long larger = (long) Math.pow(10, len) + 1; | ||
|
||
// Middle palindrome by modifying the first half | ||
long prefix = Long.parseLong(n.substring(0, (len + 1) / 2)); | ||
long candidate1 = createPalindrome(prefix, len % 2 == 0); | ||
long candidate2 = createPalindrome(prefix - 1, len % 2 == 0); | ||
long candidate3 = createPalindrome(prefix + 1, len % 2 == 0); | ||
|
||
// Collecting all candidates | ||
long[] candidates = {smaller, larger, candidate1, candidate2, candidate3}; | ||
|
||
// Finding the nearest palindrome | ||
long nearest = -1; | ||
for (long candidate : candidates) { | ||
if (candidate != num) { | ||
if (nearest == -1 || Math.abs(candidate - num) < Math.abs(nearest - num) || | ||
(Math.abs(candidate - num) == Math.abs(nearest - num) && candidate < nearest)) { | ||
nearest = candidate; | ||
} | ||
} | ||
} | ||
|
||
return String.valueOf(nearest); | ||
} | ||
|
||
private long createPalindrome(long prefix, boolean isEvenLength) { | ||
String strPrefix = String.valueOf(prefix); | ||
StringBuilder sb = new StringBuilder(strPrefix); | ||
if (!isEvenLength) { | ||
sb.setLength(sb.length() - 1); | ||
} | ||
return Long.parseLong(strPrefix + sb.reverse().toString()); | ||
} | ||
} | ||
|
||
``` | ||
|
||
### Complexity Analysis | ||
|
||
#### Time Complexity: $O(log(n))$ | ||
|
||
> **Reason**: Time Complexity is $O(log(n))$, Comparing a constant number of candidates (5 in this case) involves checking their absolute differences with the original number. | ||
|
||
#### Space Complexity: $O(1)$ | ||
|
||
> **Reason**: $O(1)$ additional space, excluding the space required to store the input and output since we only use a fixed number of variables. | ||
|
||
# References | ||
|
||
- **LeetCode Problem:** [Find the Closest Palindrome](https://leetcode.com/problems/find-the-closest-palindrome/description/) | ||
- **Solution Link:** [Find the Closest Palindrome Solution on LeetCode](https://leetcode.com/problems/find-the-closest-palindrome/solutions/) | ||
- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/vivekvardhan43862/) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.