Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
Difficulty: 60. This is a classic problem in the algorithm. Determine whether a string is a substring of another string. The most classic algorithm for this question should be the KMP algorithm. For unfamiliar friends, see knuth-Morris-Pratt algorithm. The KMP algorithm is the optimal linear algorithm, and its complexity has reached the lower limit. However, the KMP algorithm is complex and it is difficult to implement it completely and correctly in a short period of time. Therefore, the KMP algorithm is not required during the interview.
Next, let's talk about the brute force algorithm. Suppose the length of the original string is N, and the length of the matching string is M. The idea is very simple, that is, to determine whether each string with a length of M is consistent with the matching string. There are n-m + 1 substring in total, so the algorithm time complexity is O (n-m + 1) * m) = O (N * m ), the space complexity is O (1 ). The Code is as follows:
1 public class Solution { 2 public String strStr(String haystack, String needle) { 3 if (haystack == null || needle == null) return null; 4 if (needle.length() == 0) return haystack; 5 if (haystack.length() < needle.length()) return null; 6 boolean flag = false; 7 int i = 0; 8 for (; i<=haystack.length()-needle.length(); i++) { 9 String temp = haystack.substring(i, i+needle.length());10 if (temp.equals(needle)) {11 flag = true;12 break;13 }14 }15 if (flag == true) return haystack.substring(i);16 else return null;17 }18 }
You can see a linear algorithm called Rolling hash on the Internet. For more information, see rolling hash-Wikipedia. The basic idea is to use a hashcode to represent a string. To ensure the uniqueness of hash, we use a prime number larger than the character set to base on the power of this prime number. For example, the character set is a lowercase letter set with the base number 29. For example, the string "abacd" is converted to hashcode = 1 + 2*29 + 1*29 ^ 2 + 3*29 ^ 3 + 4*29 ^ 4. Then how to calculate the new hashcode in the previous step. For example, if the matching string is the original string "abacde" and the length of the matching string is 5, calculate the hashcode = H of "abacd" based on the above method, then the hashcode of "bacde" in the next step is H/29 + 5*29 ^ 4. This is a constant operation, so the time complexity of detecting all substrings only needs o (m + n-m) = O (n), and is also a linear algorithm. The Code is as follows:
1 public String strStr(String haystack, String needle) { 2 if(haystack==null || needle==null) return null; 3 if(haystack.length()==0){ 4 return needle.length()==0?"":null; 5 } 6 if(needle.length()==0) return haystack; 7 if(haystack.length()<needle.length()) return null; 8 9 int base = 29;10 long patternHash = 0;11 long tempBase = 1;12 13 for(int i=needle.length()-1; i>=0; i--){14 patternHash += (int)needle.charAt(i)*tempBase;15 tempBase *= base;16 }17 18 long hayHash = 0;19 tempBase = 1;20 for(int i=needle.length()-1; i>=0; i--){21 hayHash += (int)haystack.charAt(i)*tempBase;22 tempBase *= base;23 }24 tempBase /= base;25 26 if(hayHash == patternHash){27 return haystack;28 }29 30 for(int i=needle.length(); i
Leetcode: Implement strstr ()