Skip to content

Change pattern search mechanism #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions strings/naive_string_search.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
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:


def naivePatternSearch(mainString, pattern):
patLen = len(pattern)
strLen = len(mainString)
position = []
if patLen > strLen:
return position
for i in range(strLen - patLen + 1):
if mainString[i : i + patLen + 1] == pattern:
position.append(i)
return position

mainString="ABAAABCDBBABCDDEBCABC"
pattern="ABC"
position=naivePatternSearch(mainString,pattern)

mainString = "ABAAABCDBBABCDDEBCABC"
pattern = "ABC"
position = naivePatternSearch(mainString, pattern)
print("Pattern found in position ")
for x in position:
print(x)
print(x)