Skip to content

Commit a12882d

Browse files
refactor 214
1 parent 2c8d969 commit a12882d

File tree

1 file changed

+40
-38
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+40
-38
lines changed

src/main/java/com/fishercoder/solutions/_214.java

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
214. Shortest Palindrome
5+
56
Given a string S, you are allowed to convert it to a palindrome
67
by adding characters in front of it.
78
Find and return the shortest palindrome you can find by performing this transformation.
@@ -11,54 +12,55 @@
1112
Given "aacecaaa", return "aaacecaaa".
1213
1314
Given "abcd", return "dcbabcd".
14-
15-
1615
*/
1716
public class _214 {
1817

19-
/**credit: https://discuss.leetcode.com/topic/27261/clean-kmp-solution-with-super-detailed-explanation*/
20-
/**TODO: read it explanation and understand KMP completely.*/
21-
public String shortestPalindrome(String s) {
22-
String temp = s + "#" + new StringBuilder(s).reverse().toString();
23-
int[] table = getTable(temp);
24-
//get the maximum palin part in s starts from 0
25-
return new StringBuilder(s.substring(table[table.length - 1])).reverse().toString() + s;
26-
}
18+
public static class Solution1 {
19+
/**credit: https://discuss.leetcode.com/topic/27261/clean-kmp-solution-with-super-detailed-explanation*/
20+
/**
21+
* TODO: read it explanation and understand KMP completely.
22+
*/
23+
public String shortestPalindrome(String s) {
24+
String temp = s + "#" + new StringBuilder(s).reverse().toString();
25+
int[] table = getTable(temp);
26+
//get the maximum palin part in s starts from 0
27+
return new StringBuilder(s.substring(table[table.length - 1])).reverse().toString() + s;
28+
}
2729

28-
public int[] getTable(String s) {
29-
//get lookup table
30-
int[] table = new int[s.length()];
30+
public int[] getTable(String s) {
31+
//get lookup table
32+
int[] table = new int[s.length()];
3133

32-
//pointer that points to matched char in prefix part
33-
int index = 0;
34-
//skip index 0, we will not match a string with itself
35-
for (int i = 1; i < s.length(); i++) {
36-
if (s.charAt(index) == s.charAt(i)) {
37-
//we can extend match in prefix and postfix
38-
table[i] = table[i - 1] + 1;
39-
index++;
40-
} else {
41-
//match failed, we try to match a shorter substring
34+
//pointer that points to matched char in prefix part
35+
int index = 0;
36+
//skip index 0, we will not match a string with itself
37+
for (int i = 1; i < s.length(); i++) {
38+
if (s.charAt(index) == s.charAt(i)) {
39+
//we can extend match in prefix and postfix
40+
table[i] = table[i - 1] + 1;
41+
index++;
42+
} else {
43+
//match failed, we try to match a shorter substring
4244

43-
//by assigning index to table[i-1], we will shorten the match string length, and jump to the
44-
//prefix part that we used to match postfix ended at i - 1
45-
index = table[i - 1];
45+
//by assigning index to table[i-1], we will shorten the match string length, and jump to the
46+
//prefix part that we used to match postfix ended at i - 1
47+
index = table[i - 1];
4648

47-
while (index > 0 && s.charAt(index) != s.charAt(i)) {
48-
//we will try to shorten the match string length until we revert to the beginning of match (index 1)
49-
index = table[index - 1];
50-
}
49+
while (index > 0 && s.charAt(index) != s.charAt(i)) {
50+
//we will try to shorten the match string length until we revert to the beginning of match (index 1)
51+
index = table[index - 1];
52+
}
5153

52-
//when we are here may either found a match char or we reach the boundary and still no luck
53-
//so we need check char match
54-
if (s.charAt(index) == s.charAt(i)) {
55-
//if match, then extend one char
56-
index++;
54+
//when we are here may either found a match char or we reach the boundary and still no luck
55+
//so we need check char match
56+
if (s.charAt(index) == s.charAt(i)) {
57+
//if match, then extend one char
58+
index++;
59+
}
60+
table[i] = index;
5761
}
58-
table[i] = index;
5962
}
63+
return table;
6064
}
61-
return table;
6265
}
63-
6466
}

0 commit comments

Comments
 (0)