Answer to Question #174129 in Python for CHANDRASENA REDDY CHADA

Question #174129

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-26T01:03:24-0400

Code:


def main():
    #Read the first string from the keayboard    
    string1=input("")
    #Read the second string from the keayboard
    string2=input("")
    nRotation=getNumberRotation(string1,string2)
    #check the number of right rotations
    if nRotation==-1:
        print("No Match")
    else:
        print(str(nRotation))
        
#This function allows to count the number of right rotations
def getNumberRotation(string1,string2):
    tempString=string1
    nRotation =0
    while tempString!=string2:
        nRotation+=1
        tempString = string1[len(string1) - nRotation:] + string1[0 : len(string1) - nRotation]
        if nRotation>=len(string1):
            return -1
            
    return nRotation


main()


Examples:

>>> 
Python
Pythone
No Match
>>> 
Python
Python
0
>>> 
python
onpyth
2
>>> 

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