Day 9 | 28. Find the Index of the First Occurrence in a String | 459. Repeated Substring Pattern

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
  • LeetCode 28. Find the Index of the First Occurrence in a String
  • LeetCode 459. Repeated Substring Pattern


KMP

  • KMP was invented by Knuth, Morris and Pratt. 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 to avoid repeat matching.
  • The prefix table is 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

Question Link

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 next array 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(), i starts from 1, because i points to the start of suffix

LeetCode 459. Repeated Substring Pattern

Question Link

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 suffix doesn’t contain is the smallest repeat substring.
  • The length of array minus the length of longest public prefix suffix is 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.

你可能感兴趣的:(LeetCode,leetcode)