01.09. String Rotation
Description
Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 (e.g.,"waterbottle" is a rotation of"erbottlewat"). Can you use only one call to the method that checks if one word is a substring of another?
Example 1:
Input: s1 = "waterbottle", s2 = "erbottlewat" Output: True
Example 2:
Input: s1 = "aa", "aba" Output: False
Note:
0 <= s1.length, s1.length <= 100000
Solutions
Solution 1: String Matching
Thinking
A rotation moves a prefix to the end. Trying every cut is \(n\) comparisons of length \(n\), acceptable but redundant.
Unequal lengths cannot be rotations. When lengths match, \(s_1+s_1\) contains every rotation of \(s_1\), so the question is whether \(s_2\) is a substring of that concatenation.
The code compares lengths first, then uses s2 in s1 * 2, which is exactly that test.
First, if the lengths of strings \(s1\) and \(s2\) are not equal, they are definitely not rotation strings of each other.
Second, if the lengths of strings \(s1\) and \(s2\) are equal, then by concatenating two \(s1\) together, the resulting string \(s1 + s1\) will definitely include all rotation cases of \(s1\). At this point, we just need to check whether \(s2\) is a substring of \(s1 + s1\).
# True
s1 = "aba"
s2 = "baa"
s1 + s1 = "abaaba"
^^^
# False
s1 = "aba"
s2 = "bab"
s1 + s1 = "abaaba"
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of string \(s1\).
1 2 3 | |
1 2 3 4 5 | |
1 2 3 4 5 6 | |
1 2 3 | |
1 2 3 | |
1 2 3 4 5 | |
1 2 3 4 5 | |