Java ProgramsStringsCheck String Rotation

Check String Rotation in Java

intermediate·  Strings  ·  String

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.

Input
"carrot", "rotcar"
Output
Is rotation: true

Java Program

Java
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

Is rotation: true

Core Logic

Trying every possible rotation offset, and comparing the result against the target, checks every way the first string could have been rotated.

How It Works
  1. 1The lengths of the two strings are compared first — strings of different lengths can never be rotations of each other.
  2. 2The loop tries every offset from 0 up to n - 1, where n is the shared length.
  3. 3For each offset, str1.substring(offset) + str1.substring(0, offset) builds the string rotated left by that many characters.
  4. 4If a rotated version .equals(str2), isRotation is set to true and the loop exits with break.
For "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.

Complexity
Time Complexity: O(n²)Space Complexity: O(n)

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

String.substring()for looprotation offset

Approach 2: Concatenation Trick

Java
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

Is rotation: true

Core Logic

Every rotation of a string appears somewhere inside that string concatenated with itself — so a single substring search answers the whole question.

How It Works
  1. 1str1 + str1 concatenates the first string with itself, producing every possible rotation as a contiguous substring somewhere inside it.
  2. 2.contains(str2) checks whether the target string appears anywhere in that doubled string.
  3. 3The length check still runs first, since a string longer than str1 could otherwise coincidentally appear inside str1 + 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.

Complexity
Time Complexity: O(n²)Space Complexity: O(n)

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.

Key Concepts

String.contains()string concatenation

Related Programs