Check if One String is a Rotation of Another

Visualization Player

Problem Statement

Given two strings s1 and s2, your task is to check whether s2 is a rotation of s1.

  • A string s2 is considered a rotation of s1 if you can take some characters from the start of s1 and move them to the end, in the same order, to get s2.
  • For example, "erbottlewat" is a rotation of "waterbottle" because you can move "water" to the end to get "bottlewater" and then adjust the characters to match.

Return true if s2 is a rotation of s1, else return false.

Examples

s1 s2 Output Description
"waterbottle" "erbottlewat" true s2 is a rotation of s1
"hello" "lohel" true Characters rotated from front to back
"abcd" "cdab" true Valid rotation ("ab" moved to the end)
"abcd" "acbd" false Characters are shuffled, not rotated
"abc" "abcd" false Different lengths, cannot be rotations
"abcd" "abce" false Same length, but different characters
"" "" true Both strings are empty — considered valid rotation
"" "a" false Empty string cannot rotate to form a non-empty string
"a" "" false Non-empty string cannot match an empty one

Solution

Understanding the Problem

We are given two strings, s1 and s2, and our task is to determine whether s2 is a rotation of s1. A rotation means that the characters in s1 can be moved around in a circular fashion to form s2.

For example, if s1 = "waterbottle", then any of the following are valid rotations: "erbottlewat", "lewaterbott", "ottlewaterb", etc.

A beginner-friendly way to think about it is: you can "cut" s1 at any position and swap the two parts — if you get s2, then it's a rotation.

Step-by-Step Solution with Example

step 1: Check Lengths

If two strings are not of the same length, they can never be rotations of each other. So, the first check should be:

if (s1.length !== s2.length) return false;

step 2: Concatenate s1 with itself

The key trick is this: If you take s1 and concatenate it with itself (s1 + s1), all possible rotations of s1 will be included in this new string.

let combined = s1 + s1;

step 3: Check if s2 is a substring of combined

Now we simply check whether s2 is a substring of combined. If it is, then s2 is a rotation of s1.

return combined.includes(s2);

step 4: Try with a Real Example

Let’s say s1 = "waterbottle" and s2 = "erbottlewat".

  • Step 1: Length check – both are length 11 ✅
  • Step 2: Concatenate s1 with itself – "waterbottlewaterbottle"
  • Step 3: Check if s2 is a substring of the above – Yes, it is! ✅

So the output will be true.

Edge Cases

  • Case 1: Same strings – If s1 = s2, then it's a rotation (rotation by 0 characters).
  • Case 2: Valid rotations2 can be formed by rotating characters from the front of s1 to the back.
  • Case 3: Different lengths – Return false immediately.
  • Case 4: Same length, different characters – Even with equal lengths, mismatched content will return false.
  • Case 5: Empty strings – Two empty strings are considered valid rotations. Return true.
  • Case 6: One string empty – If only one is empty, return false.

Finally

This solution is elegant, beginner-friendly, and efficient. It avoids manual rotation checks and uses string operations that are optimized in modern programming languages.

Time Complexity: O(n) — for substring search
Space Complexity: O(n) — due to the concatenated string

The core idea — doubling the string to expose all rotations — is a clever trick worth remembering.

Algorithm Steps

  1. Check if lengths of s1 and s2 are equal. If not, return false.
  2. Concatenate s1 with itself to create a new string temp = s1 + s1.
  3. Check if s2 is a substring of temp.
  4. If yes, return true; else, return false.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int isRotation(char *s1, char *s2) {
    if (strlen(s1) != strlen(s2)) return 0;
    char *temp = malloc(strlen(s1) * 2 + 1);
    strcpy(temp, s1);
    strcat(temp, s1);
    int result = strstr(temp, s2) != NULL;
    free(temp);
    return result;
}

int main() {
    char s1[] = "abcde";
    char s2[] = "deabc";
    printf("Is Rotation: %s\n", isRotation(s1, s2) ? "true" : "false");
    return 0;
}

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(n)We check the length and then perform a substring search which in most cases is linear time.
Average CaseO(n)Checking if one string is in another (substring search) typically takes linear time in practice.
Worst CaseO(n^2)In the worst case, depending on implementation of the `in` or `contains` function, substring search can be quadratic.

Space Complexity

O(n)

Explanation: We create a temporary string which is double the size of s1, so extra space used is proportional to n.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...