Answer to Question #255796 in Python for Chethan

Question #255796
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 0
1
Expert's answer
2021-10-30T10:36:44-0400
S1=input("")
S2=input("")
numR =0
temp=S1
no_match=False
n=len(S1)
while temp!=S2:
    numR+=1
    temp = S1[n - numR:] + S1[0 : n - numR]
    if(numR>=n):
        temp=S2
        no_match=True
if no_match:
    print("No Match")
else:
    print(str(numR))

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