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
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.
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
- A shift never changes the length of a string, so consider what different lengths tell you.
- After len(s) shifts, s is back where it started, so only len(s) different results are possible.
- Zero shifts count as a valid number of shifts.