Given a string, we need to find the minimum number of rotations required to get the same string. In this case, we will only consider Left rotations.
Input : s = “geeks”
Output : 5
Input : s = “aaaa”
Naive approach: The basic approach is to keep rotating the string from the first position and count the number of rotations until we get the initial string.
Efficient Approach : We will follow the basic approach but will try to reduce the time taken in generating rotations.
The idea is as follows:
- Generate a new string of double size of the input string as:
newString = original string excluding first character + original string with the first character. + denotes concatenation here.
If orginal string is str = “abcd”, new string will be “bcdabcd”.
- Now, the task remains to search for the original string in the newly generated string and the index where the string is found in the number of rotations required.
- For string matching, we will use KMP algorithm which performs string matching in linear time.
Below is the implementation of the above approach:
Time Complexity : O(N).
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum rotations required to get the same string
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Minimum number of given operations required to convert a string to another string
- Minimum changes required to make first string substring of second string
- Minimum flips required to keep all 1s together in a Binary string
- Minimum changes required such that the string satisfies the given condition
- Minimum number of subsequences required to convert one string to another
- Minimum given operations required to convert a given binary string to all 1's
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum swaps required to convert one binary string to another
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum swaps required to make a binary string alternating
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum operations required to make the string satisfy the given condition
- Minimum number of swaps required to make the string K periodic
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Minimum cuts required to convert a palindromic string to a different palindromic string
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : divyamohan123