Skip to content

Commit efdefea

Browse files
Add Shortest Palindrome
1 parent e252c61 commit efdefea

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ Each problem has its own directory containing:
1717
| 3 | [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters) | Medium | [Java](Longest%20Substring%20Without%20Repeating%20Characters/Solution.java) | Hash Table, String, Sliding Window |
1818
| 4 | [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays) | Hard | [Java](Median%20of%20Two%20Sorted%20Arrays/Solution.java) | Array, Binary Search, Divide and Conquer |
1919
| 5 | [Longest Palindromic Substring](Longest%20Palindromic%20Substring) | Medium | [Java](Longest%20Palindromic%20Substring/Solution.java) | Two Pointers, String, Dynamic Programming |
20+
| 214 | [Shortest Palindrome](Shortest%20Palindrome) | Hard | [Java](Shortest%20Palindrome/Solution.java) | String, Rolling Hash, String Matching, Hash Function |
2021
| 2924 | [Find Champion II](Find%20Champion%20II) | Medium | [Java](Find%20Champion%20II/Solution.java) | Graph |
2122

2223
## Categories
2324

2425
### By Difficulty
2526
- Easy: 1
2627
- Medium: 4
27-
- Hard: 1
28+
- Hard: 2
2829

2930
### By Topics
3031
- Array Problems
@@ -36,6 +37,7 @@ Each problem has its own directory containing:
3637
- String Problems
3738
- [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters)
3839
- [Longest Palindromic Substring](Longest%20Palindromic%20Substring)
40+
- [Shortest Palindrome](Shortest%20Palindrome)
3941
- Dynamic Programming Problems
4042
- [Longest Palindromic Substring](Longest%20Palindromic%20Substring)
4143
- Linked List Problems
@@ -46,6 +48,8 @@ Each problem has its own directory containing:
4648
- [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays)
4749
- Graph Problems
4850
- [Find Champion II](Find%20Champion%20II)
51+
- String Matching Problems
52+
- [Shortest Palindrome](Shortest%20Palindrome)
4953

5054
## About LeetCode
5155

Shortest Palindrome/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Shortest Palindrome
2+
3+
## LeetCode Problem
4+
- Problem Number: 214
5+
- Problem Link: https://leetcode.com/problems/shortest-palindrome/
6+
- Difficulty: Hard
7+
- Topics: String, Rolling Hash, String Matching, Hash Function
8+
9+
## Problem Description
10+
Given a string s, you can convert it to a palindrome by adding characters in front of it.
11+
12+
Return the shortest palindrome you can find by performing this transformation.
13+
14+
Note:
15+
- A palindrome is a string that reads the same forward and backward
16+
- You can only add characters to the front of the string
17+
18+
## Examples
19+
20+
### Example 1:
21+
```
22+
Input: s = "aacecaaa"
23+
Output: "aaacecaaa"
24+
```
25+
26+
### Example 2:
27+
```
28+
Input: s = "abcd"
29+
Output: "dcbabcd"
30+
```
31+
32+
## Constraints
33+
- 0 <= s.length <= 5 * 10⁴
34+
- s consists of lowercase English letters only

Shortest Palindrome/Solution.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public String shortestPalindrome(String s) {
3+
if (s == null || s.length() <= 1) {
4+
return s;
5+
}
6+
7+
String reversed = new StringBuilder(s).reverse().toString();
8+
String combined = s + "#" + reversed;
9+
10+
int[] lps = computeLPS(combined);
11+
12+
int longestPrefix = lps[lps.length - 1];
13+
14+
String suffix = s.substring(longestPrefix);
15+
String prefix = new StringBuilder(suffix).reverse().toString();
16+
17+
return prefix + s;
18+
}
19+
20+
private int[] computeLPS(String s) {
21+
int n = s.length();
22+
int[] lps = new int[n];
23+
int length = 0;
24+
int i = 1;
25+
26+
while (i < n) {
27+
if (s.charAt(i) == s.charAt(length)) {
28+
length++;
29+
lps[i] = length;
30+
i++;
31+
} else {
32+
if (length > 0) {
33+
length = lps[length - 1];
34+
} else {
35+
lps[i] = 0;
36+
i++;
37+
}
38+
}
39+
}
40+
41+
return lps;
42+
}
43+
}

0 commit comments

Comments
 (0)