Skip to content

Commit

Permalink
Resolved all 5 request changes
Browse files Browse the repository at this point in the history
  • Loading branch information
GervinFung authored and Azzaare committed Aug 5, 2021
1 parent c36b6e4 commit 688c902
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/strings/kmp_substring_search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ Find if a string contain a given sub string with KMP Search, as an explanation m
As such, a detailed explanation can be found at https://towardsdatascience.com/pattern-search-with-the-knuth-morris-pratt-kmp-algorithm-8562407dba5b
Question
Questions and answers
1. Why KMP instead of naive search?
- Given n = length of string , m = length of string P
- Then naive search will have time complexity O(mn) to find all occurrences of pattern P in S
- Then, the naive search will have time complexity O(mn) to find all occurrences of pattern P in S
- However, knuth-morris-pratt (KMP) algorithm will have tim complexity of O(m+n) to find all occurrences of pattern P in S
- Hence, KMP is more efficient than naive approach
- Hence, KMP is more efficient than the naive approach
Example
```julia
GetIndexWithKMP("Hello I am Gervin", "Hello I am Gervin", false) # returns 1
GetIndexWithKMP("ABABDABACDABABCABAB", "ABABCABAB", false) # returns 11
Expand Down Expand Up @@ -97,4 +98,4 @@ end
# optional function that returns boolean if string does contain the sub-string given
function ContainSubstringWithKMP(string::String, sub_string::String, ignore_case::Bool)::Bool
return GetIndexWithKMP(string, sub_string, ignore_case) != NO_SUBSTRING_INDEX
end
end

0 comments on commit 688c902

Please sign in to comment.