|
2 | 2 |
|
3 | 3 | /**
|
4 | 4 | 214. Shortest Palindrome
|
| 5 | +
|
5 | 6 | Given a string S, you are allowed to convert it to a palindrome
|
6 | 7 | by adding characters in front of it.
|
7 | 8 | Find and return the shortest palindrome you can find by performing this transformation.
|
|
11 | 12 | Given "aacecaaa", return "aaacecaaa".
|
12 | 13 |
|
13 | 14 | Given "abcd", return "dcbabcd".
|
14 |
| -
|
15 |
| -
|
16 | 15 | */
|
17 | 16 | public class _214 {
|
18 | 17 |
|
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 | + } |
27 | 29 |
|
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()]; |
31 | 33 |
|
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 |
42 | 44 |
|
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]; |
46 | 48 |
|
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 | + } |
51 | 53 |
|
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; |
57 | 61 | }
|
58 |
| - table[i] = index; |
59 | 62 | }
|
| 63 | + return table; |
60 | 64 | }
|
61 |
| - return table; |
62 | 65 | }
|
63 |
| - |
64 | 66 | }
|
0 commit comments