How to find the number of lexicographically minimal string rotation?
For example:
S = abab, N = 2 S = abca, N = 1 S = aaaa, N = 4
I tried Duval's algorithm, it works very long. The string length of 100000000 characters.
How to find the number of lexicographically minimal string rotation?
For example:
S = abab, N = 2 S = abca, N = 1 S = aaaa, N = 4
I tried Duval's algorithm, it works very long. The string length of 100000000 characters.
Easy -- just determine the minimum period of the string. A string which is periodic in minimal period K
will produce identical (and hence lexicographically equal) strings for exactly N/K
different rotations, so whatever the lexicographic minimum is, it'll be the result of N/K
different rotations.
K
? –
Georgetown © 2022 - 2024 — McMap. All rights reserved.
O(N)
withO(1)
memory requirement. If this seems to be slow to you, give it up, there won't be anything faster! The program needs at least to read the whole string, which gives the minimum timeO(N)
! – Metacarpal