Leetcode 796.旋转字符串
1 题目描述(Leetcode题目链接)
给定两个字符串, A 和 B。
A 的旋转操作就是将 A 最左边的字符移动到最右边。 例如, 若 A = ‘abcde’,在移动一次之后结果就是’bcdea’ 。如果在若干次旋转操作之后,A 能变成B,那么返回True。
示例 1:
输入: A = 'abcde', B = 'cdeab'
输出: true
示例 2:
输入: A = 'abcde', B = 'abced'
输出: false
2 题解
遍历就可以了。
class Solution:
def rotateString(self, A: str, B: str) -> bool:
if not A and not B: return True
for i in range(len(A)):
if A[i:] + A[:i] == B:
return True
return False
首尾相接是真的秀,如果B是A旋转得到,那B必然是A+A的子串。
class Solution:
def rotateString(self, A: str, B: str) -> bool:
return len(A) == len(B) and B in (A + A)