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 <= 100s 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:
Imagine you have a word written on a strip of paper and you want to see if you can twist it into another specific word. The brute force strategy involves physically trying every single way to shift the letters around one by one and checking if it matches the target word.
Here's how the algorithm would work step-by-step:
def rotate_string(source_string, target_string):
# Handle edge case where strings differ in length because shifts cannot change length
if len(source_string) != len(target_string):
return False
# Handle empty strings which are technically valid rotations of each other
if not source_string and not target_string:
return True
current_string = source_string
# Iterate through every possible shift position up to the length of the string
for shift_index in range(len(source_string)):
# Check current rotation against target to avoid missing any valid shift state
if current_string == target_string:
return True
current_string = current_string[1:] + current_string[0]
# Exhausted all possible rotations without finding a match so return false
return FalseInstead of physically shifting letters around one by one, the optimal approach uses a clever text duplication trick to instantly reveal if the target word is a valid rotation. It checks a simple pattern matching condition that covers every possible shift combination in one go.
Here's how the algorithm would work step-by-step:
def rotate_string(source_string, target_string):
# Different lengths can never be valid rotations of one another.
if len(source_string) != len(target_string):
return False
# Doubling the string contains every single possible rotation sequence.
double_source = source_string + source_string
# Checking containment reveals if the target matches any valid shift.
return target_string in double_source| Case | How to Handle |
|---|---|
| Empty strings for both s and goal | Both lengths are zero, and string concatenation or direct comparison successfully returns true. |
| Strings of unequal lengths | A preliminary length check immediately returns false since rotation cannot alter string length. |
| Single-character strings that match or do not match | Direct equality or single shift check handles this correctly without index out of bounds errors. |
| Strings with all identical characters like aaa and aaa | The concatenation approach s plus s contains goal, correctly evaluating to true. |
| Maximum length strings up to constraint limits | Using efficient substring search or KMP algorithm ensures optimal time complexity without exceeding limits. |
| Strings that are anagrams but not valid rotations | The shift verification ensures character order is strictly preserved, returning false. |
| Null inputs for s or goal | Standard null checks at the beginning of the function prevent null pointer exceptions and handle gracefully. |
| Unicode or special character strings | Language native string concatenation and search functions correctly handle multi-byte Unicode code points. |