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.
Examples:
Input : s = “geeks”
Output : 5
Input : s = “aaaa”
Output :1
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 original 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:
C++
#include <bits/stdc++.h>
using namespace std;
void computeLPSArray( char * pat, int M, int * lps);
int KMPSearch( char * pat, char * txt)
{
int M = strlen (pat);
int N = strlen (txt);
int lps[M];
computeLPSArray(pat, M, lps);
int i = 0;
int j = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
return i - j;
j = lps[j - 1];
}
else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
void computeLPSArray( char * pat, int M, int * lps)
{
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}
int countRotations(string s)
{
string s1 = s.substr(1, s.size() - 1) + s;
char pat[s.length()], text[s1.length()];
strcpy (pat, s.c_str());
strcpy (text, s1.c_str());
return 1 + KMPSearch(pat, text);
}
int main()
{
string s1 = "geeks" ;
cout << countRotations(s1);
return 0;
}
|
Java
class GFG
{
static int KMPSearch( char []pat, char []txt)
{
int M = pat.length;
int N = txt.length;
int lps[] = new int [M];
computeLPSArray(pat, M, lps);
int i = 0 ;
int j = 0 ;
while (i < N)
{
if (pat[j] == txt[i])
{
j++;
i++;
}
if (j == M)
{
return i - j + 1 ;
}
else if (i < N && pat[j] != txt[i])
{
if (j != 0 )
j = lps[j - 1 ];
else
i = i + 1 ;
}
}
return 0 ;
}
static void computeLPSArray( char []pat, int M, int []lps)
{
int len = 0 ;
lps[ 0 ] = 0 ;
int i = 1 ;
while (i < M)
{
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0 ) {
len = lps[len - 1 ];
}
else
{
lps[i] = 0 ;
i++;
}
}
}
}
static int countRotations(String s)
{
String s1 = s.substring( 1 , s.length() - 1 ) + s;
char []pat = s.toCharArray();
char []text = s1.toCharArray();
return 1 + KMPSearch(pat, text);
}
public static void main(String []args)
{
String s1 = "geeks" ;
System.out.print(countRotations(s1));
}
}
|
Python3
def KMPSearch(pat, txt):
M = len (pat)
N = len (txt)
lps = [ 0 ] * M
computeLPSArray(pat, M, lps)
i = 0
j = 0
while i < N:
if pat[j] = = txt[i]:
j + = 1
i + = 1
if j = = M:
return i - j
j = lps[j - 1 ]
elif i < N and pat[j] ! = txt[i]:
if j ! = 0 :
j = lps[j - 1 ]
else :
i = i + 1
def computeLPSArray(pat, M, lps):
_len = 0
lps[ 0 ] = 0
i = 1
while i < M:
if pat[i] = = pat[_len]:
_len + = 1
lps[i] = _len
i + = 1
else :
if _len ! = 0 :
_len = lps[_len - 1 ]
else :
lps[i] = 0
i + = 1
def countRotations(s):
s1 = s[ 1 : len (s)] + s
pat = s[:]
text = s1[:]
return 1 + KMPSearch(pat, text)
s1 = "geeks"
print (countRotations(s1))
|
C#
using System;
class GFG{
static int KMPSearch( char []pat, char []txt)
{
int M = pat.Length;
int N = txt.Length;
int []lps = new int [M];
computeLPSArray(pat, M, lps);
int i = 0;
int j = 0;
while (i < N)
{
if (pat[j] == txt[i])
{
j++;
i++;
}
if (j == M)
{
return i - j ;
}
else if (i < N && pat[j] != txt[i])
{
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return 0;
}
static void computeLPSArray( char []pat, int M,
int []lps)
{
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M)
{
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}
static int countRotations( string s)
{
string s1 = s.Substring(1, s.Length - 1) + s;
char []pat = s.ToCharArray();
char []text = s1.ToCharArray();
return 1 + KMPSearch(pat, text);
}
public static void Main( params string []args)
{
string s1 = "geeks" ;
Console.Write(countRotations(s1));
}
}
|
Javascript
<script>
function KMPSearch(pat, txt)
{
let M = pat.length;
let N = txt.length;
let lps = new Array(M);
lps.fill(0);
computeLPSArray(pat, M, lps);
let i = 0;
let j = 0;
while (i < N)
{
if (pat[j] == txt[i])
{
j++;
i++;
}
if (j == M)
{
return i - j ;
}
else if (i < N && pat[j] != txt[i])
{
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return 0;
}
function computeLPSArray(pat, M, lps)
{
let len = 0;
lps[0] = 0;
let i = 1;
while (i < M)
{
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}
function countRotations(s)
{
let s1 = s.substring(1, s.length) + s;
let pat = s.split( '' );
let text = s1.split( '' );
return 1 + KMPSearch(pat, text);
}
let s1 = "geeks" ;
document.write(countRotations(s1));
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(N), where N is the length of the given string,
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Dec, 2022
Like Article
Save Article