-
-
Notifications
You must be signed in to change notification settings - Fork 155
Added lc solution for 214 #1127
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
ajay-dhangar
merged 2 commits into
codeharborhub:main
from
mahek0620:shortest-palindrome
Jun 14, 2024
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
163 changes: 163 additions & 0 deletions
163
dsa-solutions/lc-solutions/0200-0299/0214-Shortest-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,163 @@ | ||
--- | ||
id: shortest-palindrome | ||
title: Shortest Palindrome | ||
sidebar_label: 0214 Shortest Palindrome | ||
tags: | ||
- String | ||
- KMP Algorithm | ||
- C++ | ||
- Java | ||
- Python | ||
description: "This document provides a solution to finding the shortest palindrome by adding characters in front of the given string." | ||
--- | ||
|
||
## Problem | ||
Given a string `s`, you can convert it to a palindrome by adding characters in front of it. Return the shortest palindrome you can find by performing this transformation. | ||
|
||
### Example 1: | ||
Input: s = "aacecaaa" | ||
Output: "aaacecaaa" | ||
|
||
### Example 2: | ||
Input: s = "abcd" | ||
Output: "dcbabcd" | ||
|
||
### Constraints: | ||
- 0 <= s.length <= 5 * 10^4 | ||
- s consists of lowercase English letters only. | ||
|
||
## Solution | ||
To solve this problem efficiently, we can leverage the KMP (Knuth-Morris-Pratt) algorithm to find the longest prefix of the reversed string that matches the suffix of the original string. This allows us to determine the shortest palindrome. | ||
|
||
### Steps: | ||
1. Reverse the string `s` to get `rev_s`. | ||
2. Construct a new string `new_s = s + "#" + rev_s`. | ||
3. Compute the KMP table (prefix function) for `new_s` to find the longest prefix which is also a suffix. | ||
4. The length of this longest prefix gives us the length of characters from `rev_s` that we need to prepend to `s` to form the shortest palindrome. | ||
|
||
#### Code in Different Languages | ||
|
||
<Tabs> | ||
<TabItem value="Python" label="Python" default> | ||
<SolutionAuthor name="@mahek0620"/> | ||
```python | ||
|
||
class Solution(object): | ||
def shortestPalindrome(self, s): | ||
""" | ||
:type s: str | ||
:rtype: str | ||
""" | ||
if not s: | ||
return "" | ||
|
||
# Step 1: Reverse the string | ||
rev_s = s[::-1] | ||
|
||
# Step 2: Combine s and rev_s with a separator | ||
new_s = s + "#" + rev_s | ||
|
||
# Step 3: Compute KMP table (prefix function) for new_s | ||
n = len(new_s) | ||
kmp = [0] * n | ||
|
||
for i in range(1, n): | ||
j = kmp[i - 1] | ||
while j > 0 and new_s[i] != new_s[j]: | ||
j = kmp[j - 1] | ||
if new_s[i] == new_s[j]: | ||
j += 1 | ||
kmp[i] = j | ||
|
||
# Step 4: Calculate the overlap length | ||
overlap = kmp[-1] | ||
|
||
# Step 5: Form the result palindrome | ||
return rev_s[:len(s) - overlap] + s | ||
|
||
``` | ||
</TabItem> | ||
<TabItem value="Java" label="Java"> | ||
<SolutionAuthor name="@mahek0620"/> | ||
```java | ||
|
||
class Solution { | ||
public String shortestPalindrome(String s) { | ||
String rev_s = new StringBuilder(s).reverse().toString(); | ||
|
||
// Form the new string to check | ||
String new_s = s + "#" + rev_s; | ||
int n = new_s.length(); | ||
|
||
// Compute the KMP table (prefix function) | ||
int[] kmp = new int[n]; | ||
for (int i = 1, j = 0; i < n; ++i) { | ||
while (j > 0 && new_s.charAt(i) != new_s.charAt(j)) { | ||
j = kmp[j - 1]; | ||
} | ||
if (new_s.charAt(i) == new_s.charAt(j)) { | ||
++j; | ||
} | ||
kmp[i] = j; | ||
} | ||
|
||
// The length of the longest prefix which is also suffix | ||
int overlap = kmp[n - 1]; | ||
|
||
// Construct the result | ||
return rev_s.substring(0, rev_s.length() - overlap) + s; | ||
} | ||
} | ||
|
||
``` | ||
</TabItem> | ||
<TabItem value="C++" label="C++"> | ||
<SolutionAuthor name="@mahek0620"/> | ||
```cpp | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
string shortestPalindrome(string s) { | ||
string rev_s = s; | ||
reverse(rev_s.begin(), rev_s.end()); | ||
|
||
// Form the new string to check | ||
string new_s = s + "#" + rev_s; | ||
int n = new_s.size(); | ||
|
||
// Compute the KMP table (prefix function) | ||
vector<int> kmp(n, 0); | ||
for (int i = 1, j = 0; i < n; ++i) { | ||
while (j > 0 && new_s[i] != new_s[j]) { | ||
j = kmp[j - 1]; | ||
} | ||
if (new_s[i] == new_s[j]) { | ||
++j; | ||
} | ||
kmp[i] = j; | ||
} | ||
|
||
// The length of the longest prefix which is also suffix | ||
int overlap = kmp[n - 1]; | ||
|
||
// Construct the result | ||
return rev_s.substr(0, rev_s.size() - overlap) + s; | ||
} | ||
}; | ||
|
||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
|
||
## References | ||
|
||
- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/shortest-palindrome/) | ||
- **Solution Link:** [Shortest Palindrome Solution on LeetCode](https://leetcode.com/problems/shortest-palindrome/solutions/) | ||
- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
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.