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.
https://leetcode.com/problems/rotate-string/
Example 1:
Input:
s = "abcde", goal = "cdeab"
Output:true
Example 2:
Input:
s = "abcde", goal = "abced"
Output:false
Constraints:
1 <= s.length, goal.length <= 100sandgoalconsist of lowercase English letters.
Test Cases
1 | class Solution: |
1 | import pytest |
Thoughts
先比较 s 和 goal,如果不相同则移位一次 s 再继续比较。不用真的移动,可以利用下标。
时间复杂度 O(n²)。
Code
1 | class Solution: |