Rotate String
We are given two strings, A and B.
A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = ‘abcde’, then it will be ‘bcdea’ after one shift on A. Return True if and only if A can become B after some number of shifts on A.
Example 1:
Input: A = ‘abcde’, B = ‘cdeab’
Output: true
Example 2:
Input: A = ‘abcde’, B = ‘abced’
Output: false
Note:
A and B will have length at most 100.
Python代码:
class Solution(object):
def rotateString(self, A, B):
"""
:type A: str
:type B: str
:rtype: bool
"""
if len(A) != len(B) :
return False
else :
if len(A) == 0:
return True
for i in range(0,len(A)) :
ch = A[0]
A= A[1::] + ch
if A == B :
return True
return False
思路:
首先, 字符串长度不相等, 返回False; 字符串长度都为0返回True.
然后,将字符串首位移动到最后一位, 匹配成功则返回True; 否则,返回False.