Skip to content

Latest commit

 

History

History
71 lines (49 loc) · 1.92 KB

牛客_0114_简单_旋转字符串.md

File metadata and controls

71 lines (49 loc) · 1.92 KB

旋转字符串

last modify

旋转字符串_牛客题霸_牛客网

问题简述
字符串旋转:
给定两字符串A和B,如果能将A从中间某个位置分割为左右两部分字符串(可以为空串),并将左边的字符串移动到右边字符串后面组成新的字符串可以变为字符串B时返回true。
思路1(常规)
Python
class Solution:
    def solve(self , A: str, B: str) -> bool:
        if len(A) != len(B): return False
        
        for i in range(len(A)):
            if A[i:] + A[:i] == B: return True
            
        return False
思路2(巧妙)
  • 如果 BAA 的子串,则返回 True
Python
class Solution:
    def solve(self , A: str, B: str) -> bool:
        if len(A) != len(B): return False
        
        return B in A + A