Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e6e5f4b

Browse files
Reshad-Hasanpoyea
authored andcommitted
Added naive string search algorithm (TheAlgorithms#715)
1 parent 2b27086 commit e6e5f4b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎strings/naiveStringSearch.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
this algorithm tries to find the pattern from every position of
3+
the mainString if pattern is found from position i it add it to
4+
the answer and does the same for position i+1
5+
6+
Complexity : O(n*m)
7+
n=length of main string
8+
m=length of pattern string
9+
"""
10+
def naivePatternSearch(mainString,pattern):
11+
patLen=len(pattern)
12+
strLen=len(mainString)
13+
position=[]
14+
for i in range(strLen-patLen+1):
15+
match_found=True
16+
for j in range(patLen):
17+
if mainString[i+j]!=pattern[j]:
18+
match_found=False
19+
break
20+
if match_found:
21+
position.append(i)
22+
return position
23+
24+
mainString="ABAAABCDBBABCDDEBCABC"
25+
pattern="ABC"
26+
position=naivePatternSearch(mainString,pattern)
27+
print("Pattern found in position ")
28+
for x in position:
29+
print(x)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /