Skip to content

Commit 123e6f0

Browse files
authored
Suffix Array and LCP implementation.py
1 parent 0094577 commit 123e6f0

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

divide_and_conquer/Suffix Array and LCP implementation.py

+13-24
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ def build_suffix_array(self) -> List[int]:
2121
[5, 3, 1, 0, 4, 2]
2222
"""
2323
n = len(self.text)
24-
suffixes = sorted(range(n), key=lambda i: self.text[i:])
25-
return suffixes
24+
# Create a list of suffix indices sorted by the suffixes they point to
25+
sorted_suffix_indices = sorted(range(n), key=lambda suffix_index: self.text[suffix_index:])
26+
return sorted_suffix_indices
2627

2728
def build_lcp_array(self) -> List[int]:
2829
"""
2930
Builds the LCP (Longest Common Prefix) array for the suffix array.
30-
LCP[i] gives the length of the longest common prefix of the suffixes starting at suffix_array[i] and suffix_array[i-1].
31+
LCP[i] gives the length of the longest common prefix of the suffixes
32+
starting at suffix_array[i] and suffix_array[i-1].
3133
3234
Example:
3335
>>> sa = SuffixArray("banana")
@@ -40,20 +42,18 @@ def build_lcp_array(self) -> List[int]:
4042
lcp = [0] * n
4143

4244
# Build the rank array where rank[i] gives the position of the suffix starting at index i
43-
for i, suffix in enumerate(suffix_array):
44-
rank[suffix] = i
45+
for rank_index, suffix in enumerate(suffix_array):
46+
rank[suffix] = rank_index
4547

4648
h = 0
4749
for i in range(n):
4850
if rank[i] > 0:
49-
j = suffix_array[rank[i] - 1]
50-
while (
51-
(i + h < n) and (j + h < n) and self.text[i + h] == self.text[j + h]
52-
):
51+
j = suffix_array[rank[i] - 1] # Previous suffix in the sorted order
52+
while (i + h < n) and (j + h < n) and self.text[i + h] == self.text[j + h]:
5353
h += 1
5454
lcp[rank[i]] = h
5555
if h > 0:
56-
h -= 1
56+
h -= 1 # Decrease h for the next suffix comparison
5757
return lcp
5858

5959
def display(self) -> None:
@@ -71,26 +71,15 @@ def display(self) -> None:
7171
4: na
7272
2: nana
7373
74-
LCP Array:
75-
LCP between a and ana: 1
76-
LCP between ana and anana: 3
77-
LCP between anana and banana: 0
78-
LCP between banana and na: 0
79-
LCP between na and nana: 2
8074
"""
8175
print("Suffix Array:")
82-
for idx in self.suffix_array:
83-
print(f"{idx}: {self.text[idx:]}")
76+
for suffix_index in self.suffix_array:
77+
print(f"{suffix_index}: {self.text[suffix_index:]}")
8478

85-
print("\nLCP Array:")
86-
for i in range(1, len(self.lcp_array)):
87-
print(
88-
f"LCP between {self.text[self.suffix_array[i - 1]:]} and {self.text[self.suffix_array[i]:]}: {self.lcp_array[i]}"
89-
)
9079

9180

9281
# Example usage:
9382
if __name__ == "__main__":
9483
text = "banana"
9584
sa = SuffixArray(text)
96-
sa.display()
85+
sa.display() # Contribution for Hacktoberfest 2024

0 commit comments

Comments
 (0)