Lexicographically minimal string rotation: Difference between revisions

Content deleted Content added
Booth's algorithm was wrong (tested it in my own code), so I replaced it with a correct version, including the fix from Booth himself https://www.cs.ubc.ca/~ksbooth/PUB/LCS.shtml
Booth's Algorithm: add docstring
Line 37:
 
<syntaxhighlight lang="python">
def least_rotation(Ss: str) -> int:
"""Booth's lexicographically minimal string rotation algorithm."""
n = len(Ss)
f = [-1] * (2 * n)
k = 0
for j in range(1, 2 * n):
i = f[j - k - 1]
while i != -1 and Ss[j % n] != Ss[(k + i + 1) % n]:
if Ss[j % n] < Ss[(k + i + 1) % n]:
k = j - i - 1
i = f[i]
if i == -1 and Ss[j % n] != Ss[(k + i + 1) % n]:
if Ss[j % n] < Ss[(k + i + 1) % n]:
k = j
f[j - k] = -1