Check if strings are rotations of each other or not | Practice | GeeksforGeeks

Given two strings s1 and s2. The task is to check if s2 is a rotated version of the string s1. The characters in the strings are in lowercase.
 
Example 1:
Input:
geeksforgeeks
forgeeksgeeks
Output: 
1
Explanation: s1 is geeksforgeeks, s2 is
forgeeksgeeks. Clearly, s2 is a rotated
version of s1 as s2 can be obtained by
left-rotating s1 by 5 units.

 
Example 2:
Input:
mightandmagic
andmagicmigth
Output: 
0
Explanation: Here with any amount of
rotation s2 can't be obtained by s1.
 
Your Task:
The task is to complete the function areRotations() which checks if the two strings are rotations of each other. The function returns true if string 1 can be obtained by rotating string 2, else it returns false.
 
Expected Time Complexity: O(N).
Expected Space Complexity: O(N).
Note: N = |s1|.
 
Constraints:
1 <= |s1|, |s2| <= 107
0
shresthjaiswal13 days ago
Java Solution
Test Cases Passed:
1011 / 1011
Total Time Taken:
0.45/1.6
public static boolean areRotations(String s1, String s2 )   {       // Your code here       if(s1.length() != s2.length())           return false;                  if((s1+s1).contains(s2))           return true;              return false;          }
+2
sharvilpatil135 days ago
JAVA CODE
class Solution{   //Function to check if two strings are rotations of each other or not.   public static boolean areRotations(String s1, String s2 )   {       if(s1.length()!=s2.length()) return false;       if(s1.equals(s2)) return true;       s1 = s1+s1;       int l = s2.length();       for(int i=0; i<s1.length()-l; i++)       {           if(s1.substring(i, i+l).equals(s2)) return true;       }       return false;   }   }
0
nitishmishra9371 week ago
//Probably a lengthy code
 
class Solution{   //Function to check if two strings are rotations of each other or not.      public static boolean checkForIndex(String s1, String s2, int currentIndex) {       int s1Index = -1;       for(int j = 0; j < s1.length(); ++j) {           if(s2.charAt(currentIndex) == s1.charAt(j)) {               s1Index = j;               break;           }       }
       if(s1Index == -1) {           return false;       }              int count = 0;       for(int i = s1Index, j = currentIndex; count < s1.length(); ++i, ++j, ++count) {           if(s1.charAt(i) != s2.charAt(j)) {               return false;           }           if(i == s1.length() - 1) {               i = -1;           }           if(j == s2.length() - 1) {               j = -1;           }       }       return true;   }      public static boolean areRotations(String s1, String s2 )   {       // Your code here              if(s1.length() != s2.length()) {           return false;       }              for(int i = 0; i < s2.length(); ++i) {           if(checkForIndex(s1, s2, i)) {               return true;           }       }       return false;   }   }
+2
visionsameer391 week ago
IN PYTHON :
simplest ways:
  class Solution:      #Function to check if two strings are rotations of each other or not.   def areRotations(self,s1,s2):       if len(s1)!=len(s2):       #firstly check both string length equal or not if                                                         not return false            return False       s3=s1+s1                        # thats logic merge s1 twice  time  then assing s3       if s2 in s3:                        check if s2 in s3  if s2 in s3 return T else  Return F           return True        else:           return False#code here
+2
avi1221861 week ago
public static boolean areRotations(String s1, String s2 )
   {
       if(s1.length() != s2.length()){
           return false;
       }
       String s = s1 +s1;
       return s.contains(s2);
   }
0
krtikeytiwari122 weeks ago
JAVA SOLUTION O(N) TIME COMPLEXITY AND O(1) SPACE COMPLEXITY!
class Solution{   public static boolean areRotations(String s1, String s2 )   {       boolean res = false;       if(s1.length()!=s2.length()) return false;       if(s1.equals(s2)){           res = true;       }       for(int i=1 ; i<s1.length() ; i++){           String s = s1.substring(i,s1.length()) + s1.substring(0,i);           if(s.equals(s2)) {               res = true;               break;           }       }       return res;   }}
0
eternalclumsee3 weeks ago
#iterate in s1for i in s1:#checking if s1 has become same as s2  if s1==s2:     return 1#creating 'new' variable which stores s1[0]   new=s1[0]
#s1[0] is sliced(khachaaak)  s1=s1[1:]#(new aka previous s1[0]) is added at the end of s1  s1=s1+new
return 0
+2
solankiabhay9161 month ago
bool areRotations(string s1,string s2)   {       // Your code here       int l1=s1.length();       int l2=s2.length();       if(l1!=l2)       return false;       else       {           string temp=s1+s1;           if(temp.find(s2)!=string::npos)           return true;       }       return false;   }
-1
ashwinaditya211 month ago
Java Solution in O(N) time and O(1) space complexity.
Take two ref x and y. Keep incrementing the y till charAt(x)==charAt(y) for both the strings and keep count in matchCount variable. if matchCount reaches the value of length of the strings, then return true.
If you have check the length of the strings thrice in this way, break out of the loop and return false.
 
class Solution
{
    //Function to check if two strings are rotations of each other or not.
    public static boolean areRotations(String s1, String s2 )
    {
        // Your code here
        int length = s1.length();
        if(length != s2.length()) {
            return false;
        }
        
        int x = 0;
        int y = 0;
        int matchCount = 0;
        int loopCount = 0;
        
        while(true) {
            loopCount++;
            if(loopCount > (3*length)) {
                break;
            }
            if(matchCount == length) {
                return true;
            }
            if(s1.charAt(x) == s2.charAt(y)) {
                matchCount++;
            } else {
                y++;
                if(y==length) y=0;
                matchCount = 0;
                continue;
            }
            x++;
            y++;
            if(x==length) x=0;
            if(y==length) y=0;
        }
        return false;
    }
    
}
+2
mohammadtanveer75401 month ago
class Solution
{
    public:
    bool areRotations(string s1,string s2)
    {
        for(int i=0;i<s1.size();i++)
        {
            if(s1==s2)
                return true;
            char c = s1[0];
            s1.erase(0,1);
            s1=s1+c;
        }
        return false;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.