Given are two strings : str1 and str2. Find all the starting indexes of str1 in str2. Example: I/p = str1: abc, str2: abckdabcgfacabc O/p = [0, 5, 12]
public static List<Integer> firstMatchingIndexes(String str1, String str2) {
List<Integer> indexes = new ArrayList<>();
int end = 0, n = str2.length();
for(; end<n; end++) {
int index = str2.substring(end, n).indexOf(str1)+end;
if(index!=-1)
indexes.add(index);
else
break;
end =index + str1.length()-1;
}
return indexes;
}
But this approach uses indexOf() which internally has O(N) time complexity. Can KMP algorithm work here?
Yes, the Knuth–Morris–Pratt algorithm can be of use here.
The Wikipedia article on the Knuth–Morris–Pratt algorithm provides pseudocode for the algorithm. Here is that pseudocode ported to Java:
This outputs:
Wikipedia says about the time complexity:
And when taking into account the time needed for building the partial-match table for a pattern of length :
We can however say it is O() when ≤ . When searching with a pattern that is longer than the string to search in, the algorithm could just skip building the partial match table for such a search string and exit with an empty list (See commented code that is not present in Wikipedia's pseudocode). That way it is always O().