Strings String basics

128. Rotate String

Easy

You are given two strings s and goal. A shift moves the first letter of s to its end. For example, one shift turns "lemon" into "emonl", and a second shift turns that into "monle".

Return true if some number of shifts turns s into goal, and false otherwise. Zero shifts counts too, so when s already equals goal, the answer is true.

Example 1

Input: s = "water", goal = "terwa"

Output: true

Explanation: One shift gives "aterw", and a second shift gives "terwa".

Example 2

Input: s = "listen", goal = "silent"

Output: false

Explanation: Both words use the same letters, but none of the six shifts of "listen" spells "silent".

Example 3

Input: s = "aaa", goal = "aa"

Output: false

Explanation: A shift never changes the length of s, so it can never become a shorter string.

Check your understanding

Work out the answer for this input before you write any code.

s = "stone", goal = "stone"

Constraints

  • 1 ≤ s.length, goal.length ≤ 105
  • s and goal hold lowercase English letters only.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.