動機

solution總是會有有趣的解法,rotate之後的string一定是原string相接中的substring

Problem

Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.

A shift on s consists of moving the leftmost character of s to the rightmost position.

  • For example, if s = abcde, then it will be bcdea after one shift.

 

Example 1:

Input: s = abcde, goal = cdeabOutput: true

Example 2:

Input: s = abcde, goal = abcedOutput: false

 

Constraints:

  • 1 <= s.length, goal.length <= 100
  • s and goal consist of lowercase English letters.

Sol

其實string長度不長,所以可以直接比

class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        for k in range(len(s)):
            if s[k:]+s[:k] == goal:
                return True
        return False

這邊要提一下有趣的解法

class Solution(object):
    def rotateString(self, A, B):
        return len(A) == len(B) and B in A+A