Problem Statement:
You are given two strings S1 and S2. You have to check if S1 is the string rotation of S2.
One string is a string rotation of another, it becomes like another string if rotated by one unit.
If S1 is the string rotation of S2, print the number of right rotations require to convert the string S1 to S2. Else print the string is not the rotation of the other.
Right rotation: Right rotation means rotating the string to right by 1 unit. For eg. if the string is “copyassignment”, then its right rotation will be “tcopyassignmen”, i.e. the last character will move to first and all the rest characters will be shifted to the right by 1 unit.
For example
Input1:
S1: assignmentcopy
S2: copyassignment
Output1:
Strings are rotations of each other.
Total Rotations = 4
Input2:
S1: assignmentypoc
S2: copyassignment
Output2:
Strings are not rotations of each other.
Code for first place in Python:
# importing deque from collections module from collections import deque # declaring 2 string mystr1 = 'copyassignment' mystr2 = 'assignmentcopy' # declaring an empty list allrotations = [] # finding all rotations of mystr1 and storing them to allrotations for i in mystr1: items = deque(mystr1) items.rotate(1) mystr1 = list(items) mystr1 = ''.join(mystr1) allrotations.append(mystr1) found = False # checking the mystr2 in allrotations and printing number of rotations required for i in range(len(allrotations)): if mystr2==allrotations[i]: found = True print(i+1) else: if not found: print("No rotations found")
Input1:

Output1:

Input2:

Output2:

Also Read:
- Hyphenate Letters in Python
- Earthquake in Python | Easy Calculation
- Striped Rectangle in Python
- Perpendicular Words in Python
- Composite Number in Python
- Greatest Among Four Numbers in Python
- Reverse the sentence in Python
- Denominations in Python
- Min and max values in an array in JavaScript
- Keyboard events in JavaScript
- Reaching Ground Floor in Python
- Number of Moves in Python
- Starks Adventure in Python
- Neutralization in Python | Assignment Expert
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- First Place in Python
- Triples with Properties in Python
- Nested list Indexing Python
- Team Points in Python
- Two words combination in Python
- ID Card in Python
- Cipher with a key in Python | Assignment Expert
- Multiple of 5 in Python
- Sandglass Star in Python
- Multiple of 3 in Python | Assignment Expert
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Sum of List Elements in Python
- All possible subsets in Python
- Names and Nicknames in Python