Answer to Question #231582 in Python for bhuvana

Question #231582

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

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 the output should be 2


1
Expert's answer
2021-09-11T01:29:39-0400
s1 = input()
s2 = input()
r = 0
flag = True
if s1 == s2:
	print(f'{r}')
else:
	for i in range(len(s1)):
		s1 = s1[len(s1)-1] + s1[:len(s1)-1]
		r += 1
		if s1 == s2:
			flag = False
			print(f'{r}')
			break
	if flag:
		print(-1)

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
APPROVED BY CLIENTS