AlgoInsight

DSA problem

Rotate String

EasyStringsPattern: String Manipulationtop600AmazonGoogle

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.

In other words, you may rotate s by any amount (including zero), and the question asks whether one of those rotations equals goal. Note that both strings consist of lowercase English letters only, and a valid answer requires the two strings to have the same length.

Examples

Example 1
Input: s = "abcde", goal = "cdeab"
Output: true
Explanation: Shifting "abcde" left twice produces "cdeab", so the answer is true.
Example 2
Input: s = "abcde", goal = "abced"
Output: false
Explanation: No sequence of left shifts of "abcde" can produce "abced".

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

stringstring-matching

Browse all DSA problems