Check String Rotation in Java
Problem
One string is a rotation of another if it can be produced by moving some number of characters from the front of the original to its back, without changing their order.
Given two strings, determine whether the second is a rotation of the first.
Java Program
public class StringRotationCheck {
public static void main(String[] args) {
String str1 = "carrot";
String str2 = "rotcar";
boolean isRotation = false;
if (str1.length() == str2.length()) {
int n = str1.length();
for (int offset = 0; offset < n; offset++) {
String rotated = str1.substring(offset) + str1.substring(0, offset); // builds the string rotated left by 'offset' characters
if (rotated.equals(str2)) {
isRotation = true;
break; // found a matching rotation, no need to try more offsets
}
}
}
System.out.println("Is rotation: " + isRotation);
}
}Output
Core Logic
Trying every possible rotation offset, and comparing the result against the target, checks every way the first string could have been rotated.
- 1The lengths of the two strings are compared first — strings of different lengths can never be rotations of each other.
- 2The loop tries every
offsetfrom0up ton - 1, wherenis the shared length. - 3For each offset,
str1.substring(offset) + str1.substring(0, offset)builds the string rotated left by that many characters. - 4If a rotated version
.equals(str2),isRotationis set totrueand the loop exits withbreak.
"carrot", the offset-3 rotation is "rotcar", which matches str2 exactly.Key Point: This tries every rotation explicitly, which is straightforward to follow but does more work than necessary — there's a well-known trick that answers the same question with a single substring search.
Why: Each of the n possible rotation offsets builds a brand-new rotated string of length n to compare, so the work multiplies across offsets and comparison length.
Key Concepts
Approach 2: Concatenation Trick
public class StringRotationConcat {
public static void main(String[] args) {
String str1 = "carrot";
String str2 = "rotcar";
// Every rotation of str1 appears somewhere inside str1 + str1
boolean isRotation = str1.length() == str2.length() && (str1 + str1).contains(str2);
System.out.println("Is rotation: " + isRotation);
}
}
Output
Core Logic
Every rotation of a string appears somewhere inside that string concatenated with itself — so a single substring search answers the whole question.
- 1
str1 + str1concatenates the first string with itself, producing every possible rotation as a contiguous substring somewhere inside it. - 2
.contains(str2)checks whether the target string appears anywhere in that doubled string. - 3The length check still runs first, since a string longer than
str1could otherwise coincidentally appear insidestr1 + str1.
"carrot" + "carrot" is "carrotcarrot", which contains "rotcar" starting right after the first three letters ('car'), confirming the rotation.Key Point: This reads far more directly than manually trying every offset — the insight that 'a rotation is just a substring of the doubled string' is what makes the whole loop unnecessary.
Why: Building str1 + str1 costs O(n) time and space, but contains() still performs the same kind of worst-case scan as a manual rotation search internally.