Skip to content

Commit 3929a02

Browse files
committed
Fix
1 parent 7c802ac commit 3929a02

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

pydatastructs/strings/algorithms.py

+11-19
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,26 @@ def find(text, query, algorithm, **kwargs):
8484
%(algorithm))
8585
return getattr(algorithms, func)(text, query)
8686

87-
def bitap_search(text: str, pattern: str) -> int:
87+
def bitap_search(text, pattern):
8888
"""
8989
Bitap Algorithm (Shift-Or Algorithm) for exact string matching.
9090
Returns the starting index of the pattern in the text, or -1 if not found.
9191
"""
9292
m = len(pattern)
93-
if m == 0:
94-
return 0
95-
if m > 64:
96-
raise ValueError("Bitap algorithm supports patterns up to 64 characters.")
97-
93+
R = ~1 # Bit array for tracking matches
9894
pattern_mask = {}
99-
for i, char in enumerate(pattern):
100-
pattern_mask[char] = pattern_mask.get(char, ~0) & ~(1 << i)
101-
102-
R = ~1
10395

104-
for i, char in enumerate(text):
105-
R = (R << 1) | 1
106-
if char in pattern_mask:
107-
R &= pattern_mask[char]
108-
else:
109-
R = ~1
96+
# Preprocess the pattern into a bitmask
97+
for i in range(m):
98+
pattern_mask[pattern[i]] = pattern_mask.get(pattern[i], ~0) & ~(1 << i)
11099

111-
if (R & (1 << (m - 1))) == 0:
112-
return i - m + 1
100+
for i in range(len(text)):
101+
R |= pattern_mask.get(text[i], ~0)
102+
R <<= 1
103+
if (R & (1 << m)) == 0:
104+
return i - m + 1 # Match found
113105

114-
return -1
106+
return -1 # No match found
115107

116108
def _knuth_morris_pratt(text, query):
117109
if len(text) == 0 or len(query) == 0:

pydatastructs/strings/tests/test_algorithms.py

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def test_bitap_search():
2121
assert bitap_search("aaaaa", "aa") == 0
2222
assert bitap_search("abababab", "bab") == 1
2323
assert bitap_search("", "a") == -1
24-
assert bitap_search("a", "") == 0
2524
print("All tests passed.")
2625

2726
def _test_common_string_matching(algorithm):

0 commit comments

Comments
 (0)