Rotate String

Rotate String is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the String Manipulation Patterns pattern, in the Naive / KMP / Rabin-Karp subpattern.

Problem statement

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.

Example 1:

Input: s = "abcde", goal = "cdeab" Output: true

Example 2:

Input: s = "abcde", goal = "abced" Output: false

Constraints:

- 1 <= s.length, goal.length <= 100

- s and goal consist of lowercase English letters.

Topics and companies

Topics: String, String Matching.

Reported in interviews at Amazon, Microsoft, Oracle.

How to practise Rotate String on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Rotate String in the code editor, or read the Rotate String editorial for a worked solution with its approach and complexity analysis.

Where Rotate String sits in the DSA pattern sheet

Problems related to Rotate String

Other problems that use the same Naive / KMP / Rabin-Karp and String Manipulation Patterns techniques:

Preparing your workspace

Progress is saved as you study.