Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List
Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists
Day 6 | 242. Valid Anagram | 349. Intersection of Two Arrays | 202. Happy Numbe | 1. Two Sum
Day 7 | 454. 4Sum II | 383. Ransom Note | 15. 3Sum | 18. 4Sum
Day 8 | 344. Reverse String | 541. Reverse String II | 替换空格 | 151.Reverse Words in a String | 左旋转字符串
Directory
KMP
- KMP was invented by
Knuth,MorrisandPratt. So KMP was the abbreviation of the first letters of their names. - KMP is mainly used for
string matching. When the string doesn’t match, we can know some parts of the matched text content. Then we can exploit this information toavoid repeat matching. - The
prefix tableis used for rolling back. It records the position where the pattern string should rematch when the pattern string doesn’t match the text string.
LeetCode 28. Find the Index of the First Occurrence in a String
Solution:
class Solution {
public int strStr(String haystack, String needle) {
// When needle is null return 0 directly
if (needle.length() == 0) return 0;
// Construct a next array
int[] next = new int[needle.length()];
getNext(next, needle);
int j = 0;
for (int i = 0; i < haystack.length(); i++) {
// Mismatch, roll back
while (j > 0 && needle.charAt(j) != haystack.charAt(i))
j = next[j - 1];
// Match, j and i move backwards at the same time
if (needle.charAt(j) == haystack.charAt(i))
j++; // i increases in the loop
if (j == needle.length()) // neddle in haystack
return i - needle.length() + 1;
}
return -1;
}
// Construct the `next` array is a process that calculates the prefix table
private void getNext(int[] next, String s) {
int j = 0; // point to the end of prefix
next[0] = j; // longest equal prefix and suffix lengths
// i starts from 1, because i point to the end of suffix
for (int i = 1; i < s.length(); i++) {
// The prefix and the suffix are different
while (j > 0 && s.charAt(j) != s.charAt(i))
j = next[j - 1]; // roll back
// The prefix and suffix are identical
if (s.charAt(j) == s.charAt(i))
j++;
next[i] = j;
}
}
}
Thought:
- Construct the
nextarray is a process that calculates the prefix table i - (needle.length() - 1)is the first position where the pattern string is present in the text string.- In
getNext(),istarts from 1, becauseipoints to the start of suffix
LeetCode 459. Repeated Substring Pattern
Solution:
class Solution {
public boolean repeatedSubstringPattern(String s) {
int len = s.length();
if(len == 0)
return false;
// contruct a next array
int[] next = new int[len];
getNext(next, s);
// Determin whether it is a repeated substring
if (next[len-1] > 0 && len % (len - next[len-1]) == 0)
return true;
return false;
}
void getNext(int[] next, String s){
int j = 0;
next[0] = j;
for(int i = 1; i < s.length(); i++){
while(j > 0 && s.charAt(j) != s.charAt(i))
j = next[j-1];
if(s.charAt(j) == s.charAt(i))
j++;
next[i] = j;
}
}
}
Thoughts:
- If a string consists of repeated substrings, the substring that the
longest public prefix suffixdoesn’t contain is thesmallest repeat substring. - The
length of arrayminus thelength of longest public prefix suffixis the length of the first cycle. If this cycle’s length could be evenly divisible, it demonstrates that the whole array consist of this cycle.
这篇博客详细介绍了KMP算法,包括其原理、应用和在LeetCode中的一些实例,如28题和459题。通过构建前缀表实现高效的字符串匹配,并探讨了如何判断字符串是否由重复子串组成。KMP算法在解决字符串匹配问题时能有效避免重复匹配,提高效率。
611

被折叠的 条评论
为什么被折叠?



