Skip to content

Instantly share code, notes, and snippets.

@Chillee
Last active April 4, 2019 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chillee/6f19408fd4fee4730f1f019f5d465f71 to your computer and use it in GitHub Desktop.
Save Chillee/6f19408fd4fee4730f1f019f5d465f71 to your computer and use it in GitHub Desktop.
Minimum Rotation: O(N)
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