Recognize a Cyclic Left Shift

Quick Overview

Recognize a Cyclic Left Shift. A shift moves the first character of s to its end. Return whether zero or more shifts can produce goal.

Recognize a Cyclic Left Shift

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem A shift moves the first character of s to its end. Return whether zero or more shifts can produce goal. ```cpp class Solution { public: bool rotateString(string s, string goal); }; ``` ### Examples **Example 1:** ```text s = "water", goal = "terwa" Output: true Explanation: Two left shifts move wa to the end. ``` **Example 2:** ```text s = "abca", goal = "acba" Output: false Explanation: The four rotations are abca,bcaa,caab,aabc. ``` ### Constraints - Both string lengths are between 1 and 100. - Characters are lowercase English letters. ### Contract considerations Preserve cyclic order and the equal-length requirement. ```hint Read the boundary condition Where do all rotations appear inside a doubled string? ``` ### Boundary reminders Preserve cyclic order and the equal-length requirement.

Overview: Recognize a Cyclic Left Shift. A shift moves the first character of s to its end. Return whether zero or more shifts can produce goal.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

A shift moves the first character of s to its end. Return whether zero or more shifts can produce goal.

class Solution {
public:
    bool rotateString(string s, string goal);
};

Examples

Example 1:

s = "water", goal = "terwa"
Output: true
Explanation: Two left shifts move wa to the end.

Example 2:

s = "abca", goal = "acba"
Output: false
Explanation: The four rotations are abca,bcaa,caab,aabc.

Constraints

  • Both string lengths are between 1 and 100.
  • Characters are lowercase English letters.

Contract considerations

Preserve cyclic order and the equal-length requirement.

Boundary reminders

Preserve cyclic order and the equal-length requirement.