796. 旋转字符串

exiaohu 于 2022-04-07 发布

题目链接:796. 旋转字符串

暴力解法。

class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        if len(s) != len(goal):
            return False

        for i in range(len(s)):
            if s[i:] + s[:i] == goal:
                return True

        return False

在评论区看到一个解法,很强:

class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        return len(s) == len(goal) and goal in s + s