Skip to content

Commit 26fbe01

Browse files
committed
Fix linting issues: combine nested if statements and add type annotation
1 parent 38daa15 commit 26fbe01

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

strings/rabin_karp.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ def rabin_karp_search(
7474

7575
# Slide the pattern over text one by one
7676
for i in range(n - m + 1):
77-
# Check if hash values match
78-
if pattern_hash == text_hash:
79-
# Verify character by character to avoid spurious hits
80-
if text[i : i + m] == pattern:
81-
matches.append(i)
77+
# Check if hash values match and verify to avoid spurious hits
78+
if pattern_hash == text_hash and text[i : i + m] == pattern:
79+
matches.append(i)
8280

8381
# Calculate hash for next window (rolling hash)
8482
if i < n - m:
@@ -141,7 +139,7 @@ def rabin_karp_multiple(
141139
patterns_by_length[length] = []
142140
patterns_by_length[length].append(pattern)
143141

144-
results = {pattern: [] for pattern in patterns}
142+
results: dict[str, list[int]] = {pattern: [] for pattern in patterns}
145143

146144
# Process each group of patterns with same length
147145
for pattern_length, pattern_group in patterns_by_length.items():
@@ -169,10 +167,9 @@ def rabin_karp_multiple(
169167
for i in range(len(text) - pattern_length + 1):
170168
# Check if current hash matches any pattern hash
171169
for pattern, pattern_hash in pattern_hashes.items():
172-
if text_hash == pattern_hash:
173-
# Verify to avoid spurious hits
174-
if text[i : i + pattern_length] == pattern:
175-
results[pattern].append(i)
170+
# Verify to avoid spurious hits
171+
if text_hash == pattern_hash and text[i : i + pattern_length] == pattern:
172+
results[pattern].append(i)
176173

177174
# Calculate hash for next window
178175
if i < len(text) - pattern_length:

0 commit comments

Comments
 (0)