Skip to main content

Check if One String is Rotation of Another

Problem Statement:​

You are given two strings of equal lengths,Β s1Β andΒ s2. The task is to checkΒ ifΒ s2Β is a rotated version of the stringΒ s1.

Note:Β The characters in the strings are in lowercase.

  • Example:

    Input: s1 = "abcd", s2 = "cdab"
    Output: true
    Explanation: After 2 right rotations, s1 will become equal to s2.
    Input: s1 = "aab", s2 = "aba"
    Output: true
    Explanation: After 1 left rotation, s1 will become equal to s2.
    Input: s1 = "abcd", s2 = "acbd"
    Output: false
    Explanation: Strings are not rotations of each other.


βœ… Solution 1: Brute Force Rotation Check​

class Solution {
public:
bool areRotations(string &s1, string &s2) {
for(int i = 0; i < s1.size(); i++){
string prefix = s1.substr(i) + s1.substr(0, i); // Rotate string
if(prefix == s2)
return true;
}
return false;
}
};


πŸ“ How It Works​

  • For each index i from 0 to s1.length() - 1, you simulate a rotation of s1 by moving the prefix s1.substr(0, i) to the end.
  • If any such rotation matches s2, return true.
  • If no rotation matches, return false.

🧩 Key Concept​

You are generating all n possible rotations of s1 and comparing them with s2.


⏱️ Time & Space Complexity​

MetricValue
TimeO(nΒ²)
SpaceO(n)

Due to repeated string slicing and concatenation.


⚠️ Edge Cases​

  • Unequal lengths β†’ return false immediately.
  • Identical strings β†’ return true (rotation by 0).
  • Empty strings β†’ return true (conventionally).

βœ… Solution 2: Concatenation Trick (Optimal)​

class Solution {
public:
bool areRotations(string &s1, string &s2) {
return (s1.size() == s2.size()) && ((s1 + s1).find(s2) != string::npos);
}
};


πŸ“ How It Works​

  • If s2 is a rotation of s1, then it must be a substring of s1 + s1.
  • For example, "ABCD" and "CDAB" β†’ "ABCDABCD" contains "CDAB".

🧩 Key Concept​

  • If s2 is a rotation of s1, it must appear in the concatenated string s1 + s1.

⏱️ Time & Space Complexity​

MetricValue
TimeO(n)
SpaceO(n)

Using .find() is optimized and avoids redundant comparisons.


⚠️ Edge Cases​

  • Check for length mismatch first β€” if lengths are unequal, it cannot be a valid rotation.

πŸ’‘ Other Approaches​

MethodTimeSpaceNotes
Brute-force RotationO(nΒ²)O(n)Simulates each rotation
Concatenation + FindO(n)O(n)Optimal and elegant βœ…


πŸš€ Level Up Your Engineering Mindset

Never Study in Isolation. Join Binary Dose.

Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.

Discord
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚑
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

πŸ’‘Have handwritten notes, cheat-sheets, or want to publish a technical article?
Learn How to Contribute→