Quick Overview

Decide whether a string can be turned into a target string by repeatedly moving its first character to the end. The problem tests reasoning about string rotations, the length edge case, and the time cost of substring search.

Check whether one string can be shifted into another by moving its first character to the end

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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` moves the leftmost character of `s` to the rightmost position. For example, if `s = "abcde"`, it becomes `"bcdea"` after one shift. ### Function Signature ```python def rotate_string(s: str, goal: str) -> bool: ``` ### Rules - Zero shifts are allowed, so `True` is the answer whenever `s == goal`. - Return `False` when the two strings have different lengths, since a shift never changes the length. ### Constraints - `1 <= len(s), len(goal) <= 100` - `s` and `goal` consist of lowercase English letters. ### Examples ```text Input: s = "abcde", goal = "cdeab" Output: True ``` Two shifts do it: `"abcde"` becomes `"bcdea"`, then `"cdeab"`. ```text Input: s = "abcde", goal = "abced" Output: False ``` The five rotations of `"abcde"` are `"abcde"`, `"bcdea"`, `"cdeab"`, `"deabc"` and `"eabcd"`, and none of them is `"abced"`. ```text Input: s = "aa", goal = "a" Output: False ``` Every shift of `"aa"` still has two characters, so it can never equal the one-character `"a"`.

Overview: Decide whether a string can be turned into a target string by repeatedly moving its first character to the end. The problem tests reasoning about string rotations, the length edge case, and the time cost of substring search.

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` moves the leftmost character of `s` to the rightmost position. For example, if `s = "abcde"`, it becomes `"bcdea"` after one shift. Implement `rotate_string(s, goal)`, which returns a boolean. ### Rules - Zero shifts are allowed, so the answer is `True` whenever `s == goal`. - Return `False` when the two strings have different lengths, since a shift never changes the length. ### Examples ```text Input: s = "abcde", goal = "cdeab" Output: True ``` Two shifts do it: `"abcde"` becomes `"bcdea"`, then `"cdeab"`. ```text Input: s = "aa", goal = "a" Output: False ``` Every shift of `"aa"` still has two characters, so it can never equal the one-character `"a"`. ### Constraints - `1 <= len(s), len(goal) <= 100` - `s` and `goal` consist of lowercase English letters.

Constraints

  • 1 <= len(s), len(goal) <= 100
  • s and goal consist of lowercase English letters.

Examples

Input: ('abcde', 'cdeab')

Expected Output: True

Explanation: First source example: two shifts turn abcde into bcdea and then cdeab.

Input: ('abcde', 'abced')

Expected Output: False

Explanation: Second source example: none of the five rotations of abcde equals abced.

Hints

  1. A shift never changes the length of a string, so consider what different lengths tell you.
  2. After len(s) shifts, s is back where it started, so only len(s) different results are possible.
  3. Zero shifts count as a valid number of shifts.

Check Whether Shifting a String Can Reach a Goal

easyGeneralSoftware Engineer

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 moves the leftmost character of s to the rightmost position. For example, if s = "abcde", it becomes "bcdea" after one shift.

Implement rotate_string(s, goal), which returns a boolean.

Rules

  • Zero shifts are allowed, so the answer is True whenever s == goal.
  • Return False when the two strings have different lengths, since a shift never changes the length.

Examples

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

Two shifts do it: "abcde" becomes "bcdea", then "cdeab".

Input: s = "aa", goal = "a"
Output: False

Every shift of "aa" still has two characters, so it can never equal the one-character "a".

Constraints

  • 1 <= len(s), len(goal) <= 100
  • s and goal consist of lowercase English letters.

Examples

Example 1
Input
('abcde', 'cdeab')
Output
True
Notes
First source example: two shifts turn abcde into bcdea and then cdeab.
Example 2
Input
('abcde', 'abced')
Output
False
Notes
Second source example: none of the five rotations of abcde equals abced.

Constraints

  • •1 <= len(s), len(goal) <= 100
  • •s and goal consist of lowercase English letters.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def rotate_string(s: str, goal: str) -> bool:
Sample call
rotate_string('abcde', 'cdeab')
Language
Case 1
Input
('abcde', 'cdeab')
Expected
True
Case 2
Input
('abcde', 'abced')
Expected
False

Browse More Questions

Show the approach

Approach

Every string reachable from s is a rotation s[k:] + s[:k] for some k from 0 to n - 1, where n = len(s). After n shifts s is back to itself, so larger shift counts repeat earlier results. The rotation by k is the length-n window of s + s that starts at index k, and the only other length-n window, at index n, is s itself. So once the lengths match, goal is reachable exactly when it occurs inside s + s. The length test has to come first: a shorter or longer goal can still occur inside s + s ("a" inside "aaaa", "bcab" inside "abcabc"), and no shift can produce it because shifts keep the length. Zero shifts correspond to the window at index 0, so s == goal returns True. Repeated letters and periodic strings such as "abab" produce repeated rotations, which the substring test handles with no special case. The library substring search is quadratic in the worst case, at most about 10^4 character comparisons for n <= 100; a linear-time matcher such as KMP would bring the time to O(n).

Time complexity:
O(n^2)
Space complexity:
O(n)