Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8b0e74e

Browse files
authoredOct 19, 2024··
suffix_array_lcp.py
1 parent 8038826 commit 8b0e74e

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed
 

‎divide_and_conquer/suffix_array_lcp.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,43 @@
22

33
import doctest
44

5-
6-
def build_suffix_array(s: str) -> list[int]:
5+
def build_suffix_array(input_string: str) -> list[int]:
76
"""
87
Build the suffix array for the given string.
98
109
Parameters:
11-
s (str): The input string.
10+
input_string (str): The input string.
1211
1312
Returns:
1413
list[int]: The suffix array (a list of starting indices of
1514
suffixes in sorted order).
1615
17-
Examples:
16+
Example:
1817
>>> build_suffix_array("banana")
1918
[5, 3, 1, 0, 4, 2]
2019
"""
21-
suffixes = [(s[i:], i) for i in range(len(s))]
20+
suffixes = [(input_string[i:], i) for i in range(len(input_string))]
2221
suffixes.sort() # Sort the suffixes lexicographically
2322
suffix_array = [suffix[1] for suffix in suffixes]
2423
return suffix_array
2524

26-
27-
def build_lcp_array(s: str, suffix_array: list[int]) -> list[int]:
25+
def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
2826
"""
2927
Build the LCP array for the given string and suffix array.
3028
3129
Parameters:
32-
s (str): The input string.
30+
input_string (str): The input string.
3331
suffix_array (list[int]): The suffix array.
3432
3533
Returns:
3634
list[int]: The LCP array.
3735
38-
Examples:
39-
>>> suffix_array = build_suffix_array("banana")
40-
>>> build_lcp_array("banana", suffix_array)
36+
Example:
37+
>>> suffix_arr = build_suffix_array("banana")
38+
>>> build_lcp_array("banana", suffix_arr)
4139
[0, 1, 3, 0, 0, 2]
4240
"""
43-
n = len(s)
41+
n = len(input_string)
4442
rank = [0] * n
4543
lcp = [0] * n
4644

@@ -53,32 +51,23 @@ def build_lcp_array(s: str, suffix_array: list[int]) -> list[int]:
5351
for i in range(n):
5452
if rank[i] > 0:
5553
j = suffix_array[rank[i] - 1]
56-
while (i + h < n) and (j + h < n) and (s[i + h] == s[j + h]):
54+
while (i + h < n) and (j + h < n) and (input_string[i + h] == input_string[j + h]):
5755
h += 1
5856
lcp[rank[i]] = h
5957
if h > 0:
6058
h -= 1 # Decrease h for the next suffix
6159
return lcp
6260

63-
6461
# Example usage
6562
if __name__ == "__main__":
66-
s = "banana"
67-
suffix_array = build_suffix_array(s)
68-
lcp_array = build_lcp_array(s, suffix_array)
63+
test_string = "banana"
64+
suffix_array = build_suffix_array(test_string)
65+
lcp_array = build_lcp_array(test_string, suffix_array)
6966

7067
print("Suffix Array:")
7168
for i in range(len(suffix_array)):
72-
print(f"{suffix_array[i]}: {s[suffix_array[i]:]}")
69+
print(f"{suffix_array[i]}: {test_string[suffix_array[i]:]}")
7370

7471
print("\nLCP Array:")
7572
for i in range(1, len(lcp_array)):
76-
lcp_info = (
77-
f"LCP between {s[suffix_array[i - 1]:]} and "
78-
f"{s[suffix_array[i]]}: {lcp_array[i]}"
79-
)
80-
print(lcp_info)
81-
82-
# Run doctests
83-
if __name__ == "__main__":
84-
doctest.testmod()
73+
print(f"LCP between {test_string[suffix_array[i - 1]:]} and {test_string[suffix_array[i]]}: {lcp_array[i]}")

0 commit comments

Comments
 (0)
Please sign in to comment.