Skip to content

Commit 0041ca7

Browse files
Pattern Searching algo
1 parent f2c0b80 commit 0041ca7

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

Pattern Searching Algo/kmpalgo.py renamed to KmpAlgo.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#effective approach for pattern searching KMP(Knuth Morris Pratt) algorithm.Time complexity O(n)
2-
def search(out, pat):
2+
def kmpsearch(out, pat):
33
n=len(out)
44
m=len(pat)
55
#array for longest prefix suffix
@@ -37,6 +37,25 @@ def compute(pat,m,p):
3737
k = k + 1
3838

3939
#to find pattern from string out
40+
41+
#brute force for pattern searching .Time complexity O(m*(n-m+1))
42+
def search(pat, str):
43+
m = len(pat)
44+
n = len(str)
45+
46+
for i in range(n-m+1):
47+
j=0
48+
49+
for j in range(0, m):
50+
if (str[i + j] != pat[j]):
51+
break
52+
53+
if (j == m-1):
54+
print("Pattern found at", i)
55+
4056
out = 'abcacdbacdabdacaabcdba'
4157
pat = 'dba'
42-
search(out,pat)
58+
print("by brute force")
59+
search(pat,out)
60+
print("by kmp algo")
61+
kmpsearch(out,pat)

Pattern Searching Algo/brute force.py

-20
This file was deleted.

0 commit comments

Comments
 (0)