Python Program to find minimum number of rotations to obtain actual string Last Updated : 09 Sep, 2022 Comments Improve Suggest changes 5 Likes 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) Create Quiz Comment M MANIKANTA2 Follow 5 Improve M MANIKANTA2 Follow 5 Improve Article Tags : Python Python Programs Python string-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like