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
luvgupta76691 week ago
Simple Optimal Working Best Java Solution | No Built-In method | No Extra Space-
 
Sol 1- Using no extra space, no direct functions used-
    public static boolean areRotations(String s,String goal){
        if(s.length()!=goal.length()) { 
            return false; 
        }
        for(int i=0;i<s.length();i++){     
            if (isRotated(s, goal, i)) { 
                return true; 
            }
        }
        return false;
    }
    static boolean isRotated(String s,String goal,int rotation){
        for(int j=0; j<s.length(); j++){
            if(s.charAt(j)!=goal.charAt((j+rotation)%s.length())){
                return false; 
            }
        }
        return true;
    }
Sol 2- Using no extra space, direct functions used-
 
public static boolean areRotations(String s1,String s2){             
   return s1.length()==s2.length() && (s2+s2).contains(s1);
}
+1
swathipriya4961 week ago
def areRotations(self,s1,s2):       #code here       for i in range(0,len(s1)):           s1=s1[1:len(s1)]+s1[0]           if(s1==s2):               return True       return False           
+1
17_7_31 week ago
        if(s1.length()!=s2.length()){
           return false;
       }else{
           s1=s1+s1;
           return (s1.find(s2)!=-1)?true:false;
       }
0
neelasatyasai931 week ago
    def areRotations(self,s1,s2):      for i in range(len(s1)):          temp = s1[i:]+s1[:i]          if temp == s2:              return True      else:          return False
0
benzwrites1 week ago
class Solution:      #Function to check if two strings are rotations of each other or not.   def areRotations(self,s1,s2):       for i in range(len(s1)):           temp = s1[i::]+s1[:i]           if temp == s2:               return True       else:           return False
0
yamijalaanvay2 weeks ago
Easiest C++ Solution: O(N)
 
bool areRotations(string s1,string s2)   {       // Your code here       if(s1.length()!=s2.length()) return false;       return ((s1+s1).find(s2)!=string::npos);   }
0
mrpalashbharati2 weeks ago
bool areRotations(string s1,string s2)   {       // Your code here       if(s1.size()!=s2.size()){           return false;       }       return (s1+s1).find(s2)!=-1;   }
+2
thecodebuilder2 weeks ago
Two solutions:
Using temporary string
bool areRotations(string s1,string s2)
   {
       if(s1.length()!=s2.length())
           return false;
       for(int i=0;i<s1.length();i++){
           string temp = "";
           temp+=s1[s1.length()-1];
           s1.pop_back();
           temp+=s1;
           s1=temp;
           if(temp==s2)
               return true;
       }
       return false;
   }
Using concatenation of one string
bool areRotations(string s1,string s2)
   {
       if(s1.length()!=s2.length())
           return false;
       s1 = s1+s1;
       return (s1.find(s2)!=-1)?true:false;
   }
0
thakursahabsingh122 weeks ago
JAVA :
 
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                     if(s1.length()!=s2.length())      return false;      return (s1+s1).contains(s2);
   }   }
+1
anujparask8hq2 weeks ago
PYTHON SOLUTION   
 #Function to check if two strings are rotations of each other or not.   def areRotations(self,s1,s2):       if len(s1)==len(s2):           if s2 in (s1+s1):               return 1       return 0           #code here
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.