# 字符串检索
class Solution:
def isFlipedString(self, s1: str, s2: str) -> bool:
if len(s1) != len(s2):
return False
if s1 == '' and s2 == '':
return True
for i in range(len(s1)):
ans = (s1[i + 1:] + s1[:i + 1])
if ans == s2:
return True
ans = ''
return False
方法二:取模运算符
思路:用取模运算符,来模拟轮转指针。其余的思路基本一致,不再赘述。
# 取模运算符
class Solution:
def isFlipedString(self, s1: str, s2: str) -> bool:
n = len(s1)
if n != len(s2):
return False
if s1 == '' and s2 == '':
return True
for i in range(n):
for j in range(n):
if s1[(i + j) % n] != s2[j]:
break
else:
return True
return False
方法三:in 判断
思路:该算法非常巧妙!既然题目并不要求输出轮转的步骤,那么直接判断 s2 in (s1 + s1) 即可。
# in 判断
class Solution:
def isFlipedString(self, s1: str, s2: str) -> bool:
return len(s1) == len(s2) and s2 in (s1 + s1)