Given a string, we need to find the minimum number of rotations required to get the same string.
Input : s = "geeks" Output : 5 Input : s = "aaaa" Output : 1
The idea is based on below post.
Step 1 : Initialize result = 0 (Here result is count of rotations)
Step 2 : Take a temporary string equals to original string concatenated with itself.
Step 3 : Now take the substring of temporary string of size same as original string starting from second character (or index 1).
Step 4 : Increase the count.
Step 5 : Check whether the substring becomes equal to original string. If yes, then break the loop. Else go to step 2 and repeat it from the next index.
Time Complexity: O(n2)
Alternate Implementation in Python :
This article is contributed by Jatin Goyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum rotations required to get the same String | Set-2
- Count of rotations required to generate a sorted array
- Number of strings which starts and ends with same character after rotations
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Minimum number of pairs required to make two strings same
- Generate all rotations of a given string
- Rotations of a Binary String with Odd Value
- Maximum contiguous 1 possible in a binary string after k rotations
- Minimum changes required to make first string substring of second string
- Minimum number of given operations required to convert a string to another string
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- A Program to check if strings are rotations of each other or not
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Maximum sum of i*arr[i] among all rotations of a given array
- Check if all rows of a matrix are circular rotations of each other
- Count rotations divisible by 4
- Check if two numbers are bit rotations of each other or not
- Quickly find multiple left rotations of an array | Set 1