|
| 1 | +--- |
| 2 | +id: shortest-palindrome |
| 3 | +title: Shortest Palindrome |
| 4 | +sidebar_label: 0214 Shortest Palindrome |
| 5 | +tags: |
| 6 | +- String |
| 7 | +- KMP Algorithm |
| 8 | +- C++ |
| 9 | +- Java |
| 10 | +- Python |
| 11 | +description: "This document provides a solution to finding the shortest palindrome by adding characters in front of the given string." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem |
| 15 | +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. |
| 16 | + |
| 17 | +### Example 1: |
| 18 | +Input: s = "aacecaaa" |
| 19 | +Output: "aaacecaaa" |
| 20 | + |
| 21 | +### Example 2: |
| 22 | +Input: s = "abcd" |
| 23 | +Output: "dcbabcd" |
| 24 | + |
| 25 | +### Constraints: |
| 26 | +- $0 \leq \text{s.length} \leq 5 \times 10^4$ |
| 27 | +- s consists of lowercase English letters only. |
| 28 | + |
| 29 | +## Solution |
| 30 | +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. |
| 31 | + |
| 32 | +### Steps: |
| 33 | +1. Reverse the string `s` to get `rev_s`. |
| 34 | +2. Construct a new string `new_s = s + "#" + rev_s`. |
| 35 | +3. Compute the KMP table (prefix function) for `new_s` to find the longest prefix which is also a suffix. |
| 36 | +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. |
| 37 | + |
| 38 | +#### Code in Different Languages |
| 39 | + |
| 40 | +<Tabs> |
| 41 | + <TabItem value="Python" label="Python" default> |
| 42 | + <SolutionAuthor name="@mahek0620"/> |
| 43 | + ```python |
| 44 | + |
| 45 | + class Solution(object): |
| 46 | + def shortestPalindrome(self, s): |
| 47 | + """ |
| 48 | + :type s: str |
| 49 | + :rtype: str |
| 50 | + """ |
| 51 | + if not s: |
| 52 | + return "" |
| 53 | + |
| 54 | + # Step 1: Reverse the string |
| 55 | + rev_s = s[::-1] |
| 56 | + |
| 57 | + # Step 2: Combine s and rev_s with a separator |
| 58 | + new_s = s + "#" + rev_s |
| 59 | + |
| 60 | + # Step 3: Compute KMP table (prefix function) for new_s |
| 61 | + n = len(new_s) |
| 62 | + kmp = [0] * n |
| 63 | + |
| 64 | + for i in range(1, n): |
| 65 | + j = kmp[i - 1] |
| 66 | + while j > 0 and new_s[i] != new_s[j]: |
| 67 | + j = kmp[j - 1] |
| 68 | + if new_s[i] == new_s[j]: |
| 69 | + j += 1 |
| 70 | + kmp[i] = j |
| 71 | + |
| 72 | + # Step 4: Calculate the overlap length |
| 73 | + overlap = kmp[-1] |
| 74 | + |
| 75 | + # Step 5: Form the result palindrome |
| 76 | + return rev_s[:len(s) - overlap] + s |
| 77 | + |
| 78 | + ``` |
| 79 | + </TabItem> |
| 80 | + <TabItem value="Java" label="Java"> |
| 81 | + <SolutionAuthor name="@mahek0620"/> |
| 82 | + ```java |
| 83 | + |
| 84 | + class Solution { |
| 85 | + public String shortestPalindrome(String s) { |
| 86 | + String rev_s = new StringBuilder(s).reverse().toString(); |
| 87 | + |
| 88 | + // Form the new string to check |
| 89 | + String new_s = s + "#" + rev_s; |
| 90 | + int n = new_s.length(); |
| 91 | + |
| 92 | + // Compute the KMP table (prefix function) |
| 93 | + int[] kmp = new int[n]; |
| 94 | + for (int i = 1, j = 0; i < n; ++i) { |
| 95 | + while (j > 0 && new_s.charAt(i) != new_s.charAt(j)) { |
| 96 | + j = kmp[j - 1]; |
| 97 | + } |
| 98 | + if (new_s.charAt(i) == new_s.charAt(j)) { |
| 99 | + ++j; |
| 100 | + } |
| 101 | + kmp[i] = j; |
| 102 | + } |
| 103 | + |
| 104 | + // The length of the longest prefix which is also suffix |
| 105 | + int overlap = kmp[n - 1]; |
| 106 | + |
| 107 | + // Construct the result |
| 108 | + return rev_s.substring(0, rev_s.length() - overlap) + s; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + ``` |
| 113 | + </TabItem> |
| 114 | + <TabItem value="C++" label="C++"> |
| 115 | + <SolutionAuthor name="@mahek0620"/> |
| 116 | + ```cpp |
| 117 | + |
| 118 | + #include <string> |
| 119 | + #include <vector> |
| 120 | + |
| 121 | + using namespace std; |
| 122 | + |
| 123 | + class Solution { |
| 124 | + public: |
| 125 | + string shortestPalindrome(string s) { |
| 126 | + string rev_s = s; |
| 127 | + reverse(rev_s.begin(), rev_s.end()); |
| 128 | + |
| 129 | + // Form the new string to check |
| 130 | + string new_s = s + "#" + rev_s; |
| 131 | + int n = new_s.size(); |
| 132 | + |
| 133 | + // Compute the KMP table (prefix function) |
| 134 | + vector<int> kmp(n, 0); |
| 135 | + for (int i = 1, j = 0; i < n; ++i) { |
| 136 | + while (j > 0 && new_s[i] != new_s[j]) { |
| 137 | + j = kmp[j - 1]; |
| 138 | + } |
| 139 | + if (new_s[i] == new_s[j]) { |
| 140 | + ++j; |
| 141 | + } |
| 142 | + kmp[i] = j; |
| 143 | + } |
| 144 | + |
| 145 | + // The length of the longest prefix which is also suffix |
| 146 | + int overlap = kmp[n - 1]; |
| 147 | + |
| 148 | + // Construct the result |
| 149 | + return rev_s.substr(0, rev_s.size() - overlap) + s; |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + ``` |
| 154 | + |
| 155 | + </TabItem> |
| 156 | +</Tabs> |
| 157 | + |
| 158 | + |
| 159 | +## References |
| 160 | + |
| 161 | +- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/shortest-palindrome/) |
| 162 | +- **Solution Link:** [Shortest Palindrome Solution on LeetCode](https://leetcode.com/problems/shortest-palindrome/solutions/) |
| 163 | +- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
0 commit comments