There are several ways to deal with string rotation.
"Programming Pearls" talks about string rotation in deep, with three linear algorithms.(click here to check it)
The first one is called "Juggling algorithm", which I spent a lot time to study it, but I still can't understand the role that Great Common Divisor plays in it. Can anybody explain it in detail ?
nand the distancedare co-prime, the cgd is 1. You swap the elements in the order (0, 3, 6, 1, 4, 7, 2, 5) - all elements have been visited. If yourdis 4, the gcd is 4, too, and you swap only (0, 4) in the first cycle and you have to do that cycle again with offsets of 1, 2 and 3. In short: There are two nested loops. The product of the loop lengths must be yournand the inner loop length depends on the divisibility ofnbyd.