From e6e5f4b301a82dfbb8245add087789019c516c9c Mon Sep 17 00:00:00 2001 From: Reshad Hasan Date: Sat, 23 Feb 2019 20:18:21 +0600 Subject: [PATCH] Added naive string search algorithm (#715) --- strings/naiveStringSearch.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 strings/naiveStringSearch.py diff --git a/strings/naiveStringSearch.py b/strings/naiveStringSearch.py new file mode 100644 index 000000000000..04c0d8157b24 --- /dev/null +++ b/strings/naiveStringSearch.py @@ -0,0 +1,29 @@ +""" +this algorithm tries to find the pattern from every position of +the mainString if pattern is found from position i it add it to +the answer and does the same for position i+1 + +Complexity : O(n*m) + n=length of main string + m=length of pattern string +""" +def naivePatternSearch(mainString,pattern): + patLen=len(pattern) + strLen=len(mainString) + position=[] + for i in range(strLen-patLen+1): + match_found=True + for j in range(patLen): + if mainString[i+j]!=pattern[j]: + match_found=False + break + if match_found: + position.append(i) + return position + +mainString="ABAAABCDBBABCDDEBCABC" +pattern="ABC" +position=naivePatternSearch(mainString,pattern) +print("Pattern found in position ") +for x in position: + print(x) \ No newline at end of file