Answer to Question #175075 in Python for bhuvanesh

Question #175075

String Rotation

Given two strings(S1 and S2), write a program to determine if a string S2 is a rotation of another string S1.


Input

The first line of the input will be a string S1.

The second line of the input will be a string S2.Output


If string S2 is a rotation of another string S1, print number of right rotations made by string S1 to match the string S2. Otherwise, print "No Match".

Where right rotation means all elements are moving towards the right side by one place where the last element will be placed in the first place example, one right rotation of "python" is "npytho"


For example, if the given strings S1 and S2 are "python" and "onpyth",

The first right rotation of s1 is "npytho" which is not equal to string S2"onpyth"

The second right rotation of s2 is "onpyth" which is equal to string S2 "onpyth".

So output 2


Sample Input 1

python

onpyth


Sample Output 1

2


Sample Input 2

Python

Python


Sample Output 2



1
Expert's answer
2021-03-24T14:08:18-0400
#Get the given strings S1 and S2
s1=input("")
s2=input("")
numberRotation =0
tmp=s1
NoMatch=False
#Check two strings
while tmp!=s2:
    numberRotation+=1
    #Right rotation 
    tmp = s1[len(s1) - numberRotation:] + s1[0 : len(s1) - numberRotation]
    if(numberRotation>=len(s1)):
        tmp=s2
        NoMatch=True


#If string S2 is a rotation of another string S1,
#print number of right rotations made by string
#S1 to match the string S2. Otherwise, print "No Match".
if NoMatch:
    print("No Match")
else:
    print(str(numberRotation))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS