A rotation of a string must appear within the original string concatenated with itself, provided both lengths match.
Complexity
O(n) expected time and O(n) space.
Check whether repeated left shifts can transform one string into another.
A rotation of a string must appear within the original string concatenated with itself, provided both lengths match.
O(n) expected time and O(n) space.
package main
import "strings"
func rotateString(s string, goal string) bool {
return len(s) == len(goal) && strings.Contains(s+s, goal)
}
class Solution:
def rotateString(self, s: str, goal: str) -> bool:
return len(s) == len(goal) and goal in s + s
function rotateString(s: string, goal: string): boolean {
return s.length === goal.length && (s + s).includes(goal);
}