-
-
Notifications
You must be signed in to change notification settings - Fork 46k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added naive string search algorithm (#715)
- Loading branch information
1 parent
2b27086
commit e6e5f4b
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |