From efb809d18e88f914dbeb467382ba05cdf5646f62 Mon Sep 17 00:00:00 2001 From: mahek0620 <136893675+mahek0620@users.noreply.github.com> Date: Thu, 13 Jun 2024 13:29:54 +0530 Subject: [PATCH 1/2] Create 0214-Shortest-Palindrome.md --- .../0200-0299/0214-Shortest-Palindrome.md | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0200-0299/0214-Shortest-Palindrome.md diff --git a/dsa-solutions/lc-solutions/0200-0299/0214-Shortest-Palindrome.md b/dsa-solutions/lc-solutions/0200-0299/0214-Shortest-Palindrome.md new file mode 100644 index 000000000..a74ad72a0 --- /dev/null +++ b/dsa-solutions/lc-solutions/0200-0299/0214-Shortest-Palindrome.md @@ -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 + + + + + ```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 + + ``` + + + + ```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; + } + } + + ``` + + + + ```cpp + + #include + #include + + 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 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; + } + }; + + ``` + + + + + +## 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/) From b29333425a04081ce9de5af07959e97005aa25f3 Mon Sep 17 00:00:00 2001 From: mahek0620 <136893675+mahek0620@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:47:13 +0530 Subject: [PATCH 2/2] Update 0214-Shortest-Palindrome.md --- .../lc-solutions/0200-0299/0214-Shortest-Palindrome.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0200-0299/0214-Shortest-Palindrome.md b/dsa-solutions/lc-solutions/0200-0299/0214-Shortest-Palindrome.md index a74ad72a0..b25ad4ee3 100644 --- a/dsa-solutions/lc-solutions/0200-0299/0214-Shortest-Palindrome.md +++ b/dsa-solutions/lc-solutions/0200-0299/0214-Shortest-Palindrome.md @@ -23,7 +23,7 @@ Input: s = "abcd" Output: "dcbabcd" ### Constraints: -- 0 <= s.length <= 5 * 10^4 +- $0 \leq \text{s.length} \leq 5 \times 10^4$ - s consists of lowercase English letters only. ## Solution