Rotate String
Decide whether moving characters from the front of one string to its end can turn it into another string.
Given two strings sgoaltruesgoalfalses
Example 1
Output: true
Explanation: two shifts turn "abcde" into "bcdea" and then "cdeab".
Example 2
Output: false
The Idea
Picture the letters of "abcde" written around the rim of a wheel. Turning the wheel by one place sends a to the back, and you read "bcdea". Turn it again and you read "cdeab". After five turns the wheel shows "abcde" again. A string you can reach this way is called a rotation of s
Example 1 asks whether "cdeab" is one of them, and it is, after two shifts. Example 2 asks about "abced". It has the same letters, but d and e have swapped places, and shifting never changes the order of the letters around the wheel. So the answer is false
Look at how "cdeab" is put together: the end of ssss
In the row labelled s + sgoals
So every rotation of ss + sgoalsgoals + s
Solution Approach
Turning that idea into code means looking at the plain way of trying every shift first, then settling two things: how to search the doubled string, and why the two lengths must be compared.
Trying every shift
What is the most direct way? Shift sgoals.substring(k) + s.substring(0, k)kgoal
Searching the doubled string
How can the doubled string do the same job with less building? Build s + scontains(goal)String.containstruegoal
Can a match ever be something other than a rotation? Not when it has n characters. A run of n characters that starts at index k of s + s
Comparing the lengths
Why does the length matter? s + ssgoal
Java Solution
The method rotateString
- First, compare
withs.length() . If they differ, thegoal.length() operator stops right there and the method returns&& , without building anything.false - Otherwise, build the doubled string
, which holds every rotation ofs + s .s - Return the result of
.(s + s).contains(goal)
The maingoalssgoal
public class RotateString {
public static boolean rotateString(String s, String goal) {
// every rotation of s appears inside s + s
return s.length() == goal.length() && (s + s).contains(goal);
}
public static void main(String[] args) {
System.out.println(rotateString("abcde", "cdeab"));
System.out.println(rotateString("abcde", "abced"));
System.out.println(rotateString("abcde", "abcde"));
System.out.println(rotateString("", ""));
System.out.println(rotateString("aa", "a"));
System.out.println(("aa" + "aa").contains("a")); // without the length check
}
}
Running it prints:
true
false
true
true
false
true
Both examples come out as expected: truefalsetruetrue"" + ""
The last two lines both use "aa" and "a". With the length check, the method prints false("aa" + "aa").contains("a")true
Complexity
The work is building s + sgoals
- Time: O(n²) in the worst case, because
may start a comparison at each of about n positions inString.contains and compare up to n characters at each one. A smarter search such ass + sKMP never steps back in after a mismatch, which brings the search down to O(n).s + s - Space: O(n), because the doubled string
has 2n characters and is built only once.s + s