Skip to content

Commit bcf5e91

Browse files
committed
add ons in string directory - Bayer_Moore_Search
1 parent bd40179 commit bcf5e91

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

strings/Boyer_Moore_Search.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
This algorithm tries to find the pattern in given text using Bad Character Heuristic method.
3+
The bad-character rule considers the character in Text at which there is a mis-match. The next occurrence of that character to the left in Pattern is found, and a shift which brings that occurrence in line with the mismatched occurrence in Text is proposed. If the mismatched character does not occur to the left in Pattern, a shift is proposed that moves the entirety of Pattern past the point of mismatch
4+
5+
Complexity : O(n/m)
6+
n=length of main string
7+
m=length of pattern string
8+
"""
9+
10+
class BayerMooreSearch:
11+
def __init__(self, text, pattern):
12+
self.text, self.pattern = text, pattern
13+
self.textLen, self.patLen = len(text), len(pattern)
14+
15+
16+
""" following functions tries to find the index of given char in pattern,
17+
if not found -1 will be returned"""
18+
def matchInPattern(self, char):
19+
for i in range(self.patLen-1, -1, -1):
20+
if char == self.pattern[i]:
21+
return i
22+
return -1
23+
24+
25+
""" following functions tries to find the mis-matched character's index in text from last,
26+
If all characters are matched, -1 will be returned"""
27+
def misMatchInText(self, currentPos):
28+
for i in range(self.patLen-1, -1, -1):
29+
if self.pattern[i] != self.text[currentPos + i]:
30+
return currentPos + i
31+
return -1
32+
33+
def badCharacterHeuristic(self):
34+
positions = []
35+
for i in range(self.textLen - self.patLen + 1):
36+
misMatchIndex = self.misMatchInText(i)
37+
if misMatchIndex == -1:
38+
positions.append(i)
39+
else:
40+
matchIndex = self.matchInPattern(self.text[misMatchIndex])
41+
i = misMatchIndex - matchIndex #shifting
42+
return positions
43+
44+
45+
if __name__ == '__main__':
46+
text = "ABAAABCDBBABCDDEBCABC"
47+
pattern = "ABC"
48+
bms = BayerMooreSearch(text, pattern)
49+
positions = bms.badCharacterHeuristic()
50+
if(len(positions) == 0):
51+
print("No match found")
52+
else:
53+
print("Pattern found in following positions: ")
54+
print(positions)
55+
56+

0 commit comments

Comments
 (0)