Data Structure & Algorithm: Level-1 Problem #2. String Rotating Shift

Given two strings, check whether one string could be rotated so that it matches the second string. Every time rotating the string, the first letter becomes the last one, the second letter becomes the first one, and the other letters follow suit.

Sample Input:

asdfghj dfghjas

Sample Output:

True

For the above sample input, string “asdfghj” could be shifted two letters to match with “dfghjas”.


数据结构和算法:初级练习题# 2 – 字符串轮转位移


给定两个字符串,检查第一个字符串进行若干次轮转位移后是否会和第二个字符串相同。每次轮转位移时,首字母变成尾字母,第二个字母变成首字母,其他字母以此类推。

输入样例:

asdfghj dfghjas

输出样例:

True

上面的输入样例里,第一个字符串“asdfghj”轮转位移两次后,就变成了第二个字符串“dfghjas”。


Python Solution

Rotate String