Python3 Program to Check if a string can be obtained by rotating another string 2 places Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given two strings, the task is to find if a string can be obtained by rotating another string two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwiseAsked in: Amazon Interview1- There can be only two cases: a) Clockwise rotated b) Anti-clockwise rotated2- If clockwise rotated that means elements are shifted in right. So, check if a substring[2.... len-1] of string2 when concatenated with substring[0,1] of string2 is equal to string1. Then, return true.3- Else, check if it is rotated anti-clockwise that means elements are shifted to left. So, check if concatenation of substring[len-2, len-1] with substring[0....len-3] makes it equals to string1. Then return true.4- Else, return false.Below is the implementation of the above approach. Python # Python 3 program to check if a string # is two time rotation of another string. # Function to check if string2 is # obtained by string 1 def isRotated(str1, str2): if (len(str1) != len(str2)): return False if(len(str1) < 2): return str1 == str2 clock_rot = "" anticlock_rot = "" l = len(str2) # Initialize string as anti-clockwise # rotation anticlock_rot = (anticlock_rot + str2[l - 2:] + str2[0: l - 2]) # Initialize string as clock wise # rotation clock_rot = clock_rot + str2[2:] + str2[0:2] # check if any of them is equal to string1 return (str1 == clock_rot or str1 == anticlock_rot) # Driver code if __name__ == "__main__": str1 = "geeks" str2 = "eksge" if isRotated(str1, str2): print("Yes") else: print("No") # This code is contributed by ita_c Output: YesTime Complexity: O(n), where n is the size of the given strings.Please refer complete article on Check if a string can be obtained by rotating another string 2 places for more details! Create Quiz Comment K kartik Follow 0 Improve K kartik Follow 0 Improve Article Tags : Python Amazon Accolite rotation 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