Last active
April 4, 2019 02:19
-
-
Save Chillee/6f19408fd4fee4730f1f019f5d465f71 to your computer and use it in GitHub Desktop.
Minimum Rotation: O(N)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int min_rotation(string s) { | |
int a = 0, N = s.size(); | |
s += s; | |
for (int b = 0; b < N; b++) | |
for (int i = 0; i < N; i++) { | |
if (a + i == b || s[a + i] < s[b + i]) { | |
b += max(0, i - 1); | |
break; | |
} | |
if (s[a + i] > s[b + i]) { | |
a = b; | |
break; | |
} | |
} | |
return a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment