Python Program to find minimum number of rotations to obtain actual string Last Updated : 09 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given two strings s1 and s2. The task is to find out the minimum number of string rotations for the given string s1 to obtain the actual string s2. Examples: Input : eeksg, geeks Output: 1 Explanation: g is rotated left to obtain geeks. Input : eksge, geeks Output: 2 Explanation : e and g are left rotated to obtain geeks. Approach: Use string slicing to rotate the string.Perform right rotations str1=str1[1:len(str1)]+str1[0] on string to obtain the actual string.Perform left rotations m=m[len(m)-1]+m[:len(m)-1] on string to obtain the actual string.Print the minimum of left(x) and right(y) rotations. TIME COMPLEXITY: O(n) Below is the implementation: python3 def findRotations(str1, str2): # To count left rotations # of string x = 0 # To count right rotations # of string y = 0 m = str1 while True: # left rotating the string m = m[len(m)-1] + m[:len(m)-1] # checking if rotated and # actual string are equal. if(m == str2): x += 1 break else: x += 1 if x > len(str2) : break while True: # right rotating the string str1 = str1[1:len(str1)]+str1[0] # checking if rotated and actual # string are equal. if(str1 == str2): y += 1 break else: y += 1 if y > len(str2): break if x < len(str2): # printing the minimum # number of rotations. print(min(x,y)) else: print("given strings are not of same kind") # Driver code findRotations('sgeek', 'geeks') Output:1 The Time and Space Complexity for all the methods are the same: Time Complexity: O(n) Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Python Program to find minimum number of rotations to obtain actual string M MANIKANTA2 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python3 Program to Find Maximum value possible by rotating digits of a given number Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N.Examples:Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765.Input: N = 7092Output: 9270Explanation: 2 min read Python3 Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31 2 min read Python3 Program for Minimum move to end operations to make all strings equal Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end.Examples: Input : n = 2 arr[] = {"molzv", "lzvmo"}Output : 2Explanation: In first string, we removefirst element("m") from first st 2 min read Python3 Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S.Examples:Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; a 5 min read Python3 Program to Find Lexicographically minimum string rotation | Set 1 Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDAD.Source: Google Written TestMore Examples: Input: GEEKSQUIZOutput: EEKSQUIZGInput: GFGOutput: FGGInput: GEEKSFORGEEKSOutput: EEKSFORGEEKSGFollowing is a simple solution. 2 min read Like