Skip to content

Commit 8dcffa3

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent a4073ca commit 8dcffa3

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

divide_and_conquer/suffix_array_lcp.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import doctest
44

5+
56
def build_suffix_array(input_string: str) -> list[int]:
67
"""
78
Build the suffix array for the given string.
@@ -22,6 +23,7 @@ def build_suffix_array(input_string: str) -> list[int]:
2223
suffix_array = [suffix[1] for suffix in suffixes]
2324
return suffix_array
2425

26+
2527
def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
2628
"""
2729
Build the LCP array for the given string and suffix array.
@@ -51,13 +53,18 @@ def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
5153
for i in range(n):
5254
if rank[i] > 0:
5355
j = suffix_array[rank[i] - 1]
54-
while (i + h < n) and (j + h < n) and (input_string[i + h] == input_string[j + h]):
56+
while (
57+
(i + h < n)
58+
and (j + h < n)
59+
and (input_string[i + h] == input_string[j + h])
60+
):
5561
h += 1
5662
lcp[rank[i]] = h
5763
if h > 0:
5864
h -= 1 # Decrease h for the next suffix
5965
return lcp
6066

67+
6168
# Example usage
6269
if __name__ == "__main__":
6370
s = "banana"
@@ -70,8 +77,10 @@ def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
7077

7178
print("\nLCP Array:")
7279
for i in range(1, len(lcp_array)):
73-
lcp_info = (f"LCP between {s[suffix_array[i - 1]:]} and "
74-
f"{s[suffix_array[i]]}: {lcp_array[i]}")
80+
lcp_info = (
81+
f"LCP between {s[suffix_array[i - 1]:]} and "
82+
f"{s[suffix_array[i]]}: {lcp_array[i]}"
83+
)
7584
print(lcp_info)
7685

7786
# Run doctests

0 commit comments

Comments
 (0)