Generate all rotations of a given string
Last Updated :
14 Sep, 2023
Given a string S. The task is to print all the possible rotated strings of the given string.
Examples:
Input : S = "geeks"
Output : geeks
eeksg
eksge
ksgee
sgeek
Input : S = "abc"
Output : abc
bca
cab
Method 1 (Simple): The idea is to run a loop from i = 0 to n – 1 ( n = length of string) i.e for each point of rotation, copy the second part of the string in the temporary string and then copy the first part of the original string to the temporary string.
Below is implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void printRotatedString( char str[])
{
int len = strlen (str);
char temp[len];
for ( int i = 0; i < len; i++)
{
int j = i;
int k = 0;
while (str[j] != '\0' )
{
temp[k] = str[j];
k++;
j++;
}
j = 0;
while (j < i)
{
temp[k] = str[j];
j++;
k++;
}
printf ( "%s\n" , temp);
}
}
int main()
{
char str[] = "geeks" ;
printRotatedString(str);
return 0;
}
|
Java
class Test
{
static void printRotatedString(String str)
{
int len = str.length();
StringBuffer sb;
for ( int i = 0 ; i < len; i++)
{
sb = new StringBuffer();
int j = i;
int k = 0 ;
for ( int k2 = j; k2 < str.length(); k2++) {
sb.insert(k, str.charAt(j));
k++;
j++;
}
j = 0 ;
while (j < i)
{
sb.insert(k, str.charAt(j));
j++;
k++;
}
System.out.println(sb);
}
}
public static void main(String[] args)
{
String str = new String( "geeks" );
printRotatedString(str);
}
}
|
Python3
def printRotatedString( str ):
lenn = len ( str )
temp = [ 0 ] * (lenn)
for i in range (lenn):
j = i
k = 0
while (j < len ( str )):
temp[k] = str [j]
k + = 1
j + = 1
j = 0
while (j < i) :
temp[k] = str [j]
j + = 1
k + = 1
print ( * temp, sep = "")
if __name__ = = '__main__' :
str = "geeks"
printRotatedString( str )
|
C#
using System;
using System.Text;
class GFG
{
public static void printRotatedString( string str)
{
int len = str.Length;
StringBuilder sb;
for ( int i = 0; i < len; i++)
{
sb = new StringBuilder();
int j = i;
int k = 0;
for ( int k2 = j; k2 < str.Length; k2++)
{
sb.Insert(k, str[j]);
k++;
j++;
}
j = 0;
while (j < i)
{
sb.Insert(k, str[j]);
j++;
k++;
}
Console.WriteLine(sb);
}
}
public static void Main( string [] args)
{
string str = "geeks" ;
printRotatedString(str);
}
}
|
PHP
<?php
function printRotatedString( $str )
{
$len = strlen ( $str );
$temp = " " ;
for ( $i = 0; $i < $len ; $i ++)
{
$j = $i ;
$k = 0;
while ( $j < $len )
{
$temp [ $k ] = $str [ $j ];
$k ++;
$j ++;
}
$j = 0;
while ( $j < $i )
{
$temp [ $k ] = $str [ $j ];
$j ++;
$k ++;
}
echo $temp . "\n" ;
}
}
$str = "geeks" ;
printRotatedString( $str );
?>
|
Javascript
<script>
function printRotatedString(str)
{
var len = str.length;
var sb;
for (i = 0; i < len; i++)
{
sb = [];
var j = i;
var k = 0;
for (k2 = j; k2 < str.length; k2++)
{
sb.push(str.charAt(j));
k++;
j++;
}
j = 0;
while (j < i)
{
sb.push(str.charAt(j));
j++;
k++;
}
document.write(sb.join( "" ) + "<br>" );
}
}
var str = "geeks" ;
printRotatedString(str);
</script>
|
Output
geeks
eeksg
eksge
ksgee
sgeek
Time complexity: O(n2) where n is length of string
Auxiliary Space: O(n)
Method 2 (Tricky and Efficient): The idea is based on the efficient method to check if strings are rotations of each other or not. We concatenate str with itself, i.e., we do str.str where . is concatenation operator. Now we traverse the concatenated string from 0 to n – 1 and print all substrings of size n.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void printRotatedString( char str[])
{
int n = strlen (str);
char temp[2*n + 1];
strcpy (temp, str);
strcat (temp, str);
for ( int i = 0; i < n; i++)
{
for ( int j=0; j != n; j++)
printf ( "%c" ,temp[i + j]);
printf ( "\n" );
}
}
int main()
{
char str[] = "geeks" ;
printRotatedString(str);
return 0;
}
|
Java
class Test
{
static void printRotatedString(String str)
{
int n = str.length();
StringBuffer sb = new StringBuffer(str);
sb.append(str);
for ( int i = 0 ; i < n; i++)
{
for ( int j= 0 ; j != n; j++)
System.out.print(sb.charAt(i + j));
System.out.println();
}
}
public static void main(String[] args)
{
String str = new String( "geeks" );
printRotatedString(str);
}
}
|
Python3
def printRotatedString(string) :
n = len (string)
temp = string + string
for i in range (n) :
for j in range (n) :
print (temp[i + j], end = "")
print ()
if __name__ = = "__main__" :
string = "geeks"
printRotatedString(string)
|
C#
using System;
using System.Text;
class Test
{
static void printRotatedString(String str)
{
int n = str.Length;
StringBuilder sb = new StringBuilder(str);
sb.Append(str);
for ( int i = 0; i < n; i++)
{
for ( int j=0; j != n; j++)
Console.Write(sb[i + j]);
Console.WriteLine();
}
}
public static void Main(String[] args)
{
String str = "geeks" ;
printRotatedString(str);
}
}
|
PHP
<?php
function printRotatedString( $str )
{
$n = strlen ( $str );
$temp = $str . $str ;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j != $n ; $j ++)
print ( $temp [ $i + $j ]);
print ( "\n" );
}
}
$str = "geeks" ;
printRotatedString( $str );
?>
|
Javascript
<script>
function printRotatedString(str)
{
var n = str.length;
var sb = str;
sb += (str);
for ( var i = 0; i < n; i++)
{
for ( var j = 0; j != n; j++)
document.write(sb.charAt(i + j));
document.write( '<br>' );
}
}
var str = "geeks" ;
printRotatedString(str);
</script>
|
Output
geeks
eeksg
eksge
ksgee
sgeek
Time complexity: O(n2) where n is length of string
Auxiliary Space : O(n)
Share your thoughts in the comments
Please Login to comment...