@@ -21,13 +21,15 @@ def build_suffix_array(self) -> List[int]:
21
21
[5, 3, 1, 0, 4, 2]
22
22
"""
23
23
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
26
27
27
28
def build_lcp_array (self ) -> List [int ]:
28
29
"""
29
30
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].
31
33
32
34
Example:
33
35
>>> sa = SuffixArray("banana")
@@ -40,20 +42,18 @@ def build_lcp_array(self) -> List[int]:
40
42
lcp = [0 ] * n
41
43
42
44
# 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
45
47
46
48
h = 0
47
49
for i in range (n ):
48
50
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 ]:
53
53
h += 1
54
54
lcp [rank [i ]] = h
55
55
if h > 0 :
56
- h -= 1
56
+ h -= 1 # Decrease h for the next suffix comparison
57
57
return lcp
58
58
59
59
def display (self ) -> None :
@@ -71,26 +71,15 @@ def display(self) -> None:
71
71
4: na
72
72
2: nana
73
73
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
80
74
"""
81
75
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 :]} " )
84
78
85
- print ("\n LCP 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
- )
90
79
91
80
92
81
# Example usage:
93
82
if __name__ == "__main__" :
94
83
text = "banana"
95
84
sa = SuffixArray (text )
96
- sa .display ()
85
+ sa .display () # Contribution for Hacktoberfest 2024
0 commit comments