09 Sept 2026Go / Python / TypeScriptEasy

Rotate String

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.

complexity

O(n) expected time and O(n) space.

solution files

  • Go rotate-string/solution.go
  • Python rotate-string/solution.py
  • TypeScript rotate-string/solution.ts

Solution files

Gorotate-string/solution.go
package main

import "strings"

func rotateString(s string, goal string) bool {
	return len(s) == len(goal) && strings.Contains(s+s, goal)
}
Pythonrotate-string/solution.py
class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        return len(s) == len(goal) and goal in s + s
TypeScriptrotate-string/solution.ts
function rotateString(s: string, goal: string): boolean {
  return s.length === goal.length && (s + s).includes(goal);
}