I tried solving this problem in SPOJ using Booth Algorithm in O(n) time, but it failed though it worked for all test cases I tried.
Then I did in Brute force way in O(n^2) time, it worked. I have attached the code for both the cases, tell me where I went wrong or is Booth algo a correct approach for this problem?
Isnt the problem, finding the minimum rotation for Lexicographically smallest string
For first approach, Booth Algorithm : http://ideone.com/J5gl5
For second approach, Brute Force : http://ideone.com/ofTeA
Your algorithm gives the wrong answer for string “ABAED”, for example.
Your algorithm returns 7 (even though this is longer than the string!).
The correct answer is 0.
(Note this bug may also be present in wherever you found a description of the algorithm! If you look at the history/discussion for the wikipedia article there are a lot of edits fixing bugs – both claiming to fix bugs in the original paper, and to fix bugs in bugfix…)
It seems to work a lot better if you replace:
with