Java String Program · S22
Check If a String Is a Rotation of Another in Java
If you move some characters from the front of a string to the back, you get a rotation. The neat trick: every rotation of a appears inside a + a.
Problem Statement
Write a method that returns true if string b can be obtained by rotating string a, that is, by moving a prefix of a to its end.
Both strings must have the same length. Two empty strings count as rotations of each other.
Input/Output Examples
"rotation", "tationro"OutputtrueExplanation: moving "ro" from the front to the back gives tationro."abcd", "acbd"OutputfalseExplanation: no rotation of abcd produces acbd."ab", "abab"OutputfalseExplanation: a rotation never changes the length.Visual Explanation
"rotation" is a window of length 8 inside "rotationrotation". The window starting at index 2 (purple) is exactly "tationro".A rotation splits a into two parts, x and y, and returns yx. Since a + a = xyxy, the string yx always sits in the middle of it.
Approaches
The first approach uses the concatenation trick. The second checks every possible rotation without building a new string.
Approach 1: Concatenate and contains() (Recommended)
Idea: check that the lengths match and that a + a contains b.
- Return
falseif the lengths differ. - Build
a + a. - Return
(a + a).contains(b).
Time: O(n) to O(n²) · Space: O(n). contains() uses a simple search; its worst case is quadratic, but it is fast in practice.
Approach 2: Try Every Shift
Idea: for each shift from 0 to n - 1, compare a[(i + shift) % n] with b[i].
- Return
falseif the lengths differ,trueif both are empty. - For each shift, compare all characters using the modulo index.
- Return
trueas soon as one shift matches completely.
Time: O(n²) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Concatenate and contains() (Recommended)
One line of logic. The length check is essential: without it, "ab" and "b" would pass.
public class RotationConcat {
// Every rotation of a is a substring of a + a
static boolean isRotation(String a, String b) {
return a.length() == b.length() && (a + a).contains(b);
}
public static void main(String[] args) {
String[][] pairs = {{"rotation", "tationro"}, {"abcd", "acbd"}, {"ab", "abab"}, {"", ""}};
for (String[] p : pairs) {
System.out.println("\"" + p[0] + "\", \"" + p[1] + "\" -> " + isRotation(p[0], p[1]));
}
}
}
"rotation", "tationro" -> true "abcd", "acbd" -> false "ab", "abab" -> false "", "" -> true
Time complexity: O(n) to O(n²) · Space complexity: O(n).
Approach 2: Try Every Shift
No extra string is allocated. It shows the definition of rotation directly, at the cost of quadratic time in the worst case.
public class RotationShift {
// Tries every shift and compares character by character, without building a + a
static boolean isRotation(String a, String b) {
int n = a.length();
if (n != b.length()) {
return false;
}
if (n == 0) {
return true;
}
for (int shift = 0; shift < n; shift++) {
int i = 0;
while (i < n && a.charAt((i + shift) % n) == b.charAt(i)) {
i++;
}
if (i == n) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String[][] pairs = {{"rotation", "tationro"}, {"abcd", "acbd"}, {"ab", "abab"}, {"", ""}};
for (String[] p : pairs) {
System.out.println("\"" + p[0] + "\", \"" + p[1] + "\" -> " + isRotation(p[0], p[1]));
}
}
}
"rotation", "tationro" -> true "abcd", "acbd" -> false "ab", "abab" -> false "", "" -> true
Time complexity: O(n²) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Concatenate and contains() | O(n) to O(n²) | O(n) |
| Try Every Shift | O(n²) | O(1) |
Here n is the length of the strings. Building a + a needs O(n) memory; checking every shift uses constant memory but may compare up to n² characters.
Interview Tips and Recommended Approach
- Never skip the length check.
(a + a).contains(b)alone accepts any substring ofa + a. - Explain why the trick works. Writing
a = xyand the rotation asyxmakes the proof one line. - Mention guaranteed linear time. Using KMP for the substring search makes the check
O(n)in the worst case. - Test equal strings. A string is a rotation of itself (shift 0).
Recommended Approach: Concatenate and contains()
Why: it is short, clearly correct and fast in practice. It is the answer most interviewers expect, followed by a discussion of why it works.