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.
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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to see if one string can become another by shifting its characters around. The brute force method involves trying every possible shift to see if any of them match the target string.
Here's how the algorithm would work step-by-step:
def rotate_string_brute_force(first_string, second_string):
string_length = len(first_string)
# Strings of different lengths can't be rotations of each other
if len(second_string) != string_length:
return False
for shift_amount in range(string_length):
# Perform the rotation
rotated_string = first_string[shift_amount:] + first_string[:shift_amount]
# Check if the rotated string matches the target
if rotated_string == second_string:
return True
# If no rotation matches, return False
return False
The key insight is that if one string is a rotation of another, it must exist as a substring within the doubled version of the original string. We create a longer string, and then perform a single search.
Here's how the algorithm would work step-by-step:
def rotate_string(original_string, rotated_string):
# Check for edge cases where rotation is impossible
if len(original_string) != len(rotated_string):
return False
doubled_original_string = original_string + original_string
# If rotated_string is a rotation, it must be a substring
if rotated_string in doubled_original_string:
return True
return False
Case | How to Handle |
---|---|
Both s and goal are null or empty strings | Return true since an empty string can be considered a shifted version of itself. |
s is null or empty, but goal is not (or vice-versa) | Return false since an empty string cannot be shifted to a non-empty string. |
s and goal have different lengths | Return false because strings of different lengths cannot be rotations of each other. |
s and goal are identical strings | Return true because a string is a rotation of itself (zero shifts). |
s and goal are very long strings (performance implications) | Ensure solution has linear time complexity (e.g., by checking if goal is a substring of s+s). |
s contains repeated characters, and goal contains the same characters but in a rotated order. | The substring check will correctly identify the rotation in linear time. |
s and goal are anagrams of each other but not rotations | The substring check `goal in s + s` will not identify anagrams as rotations; it requires the specific shifted sequence. |
Very large strings causing memory issues if s+s is created. | Consider using rolling hash or KMP algorithm for substring search, avoiding string concatenation and minimizing memory usage. |