Check if string is rotated by two places | Practice | GeeksforGeeks

Given two strings a and b. The task is to find if the string 'b' can be obtained by rotating another string 'a' by exactly 2 places.
Example 1:
Input:
a = amazon
b = azonam
Output: 1
Explanation: amazon can be rotated anti
clockwise by two places, which will make
it as azonam.

Example 2:
Input:
a = geeksforgeeks
b = geeksgeeksfor
Output: 0
Explanation: If we rotate geeksforgeeks by
two place in any direction , we won't get
geeksgeeksfor.
Your Task:
The task is to complete the function isRotated() which takes two strings as input parameters and checks if given strings can be formed by rotations. The function returns true if string 1 can be obtained by rotating string 2 by two places, else it returns false.
Expected Time Complexity: O(N).
Expected Auxilary Complexity: O(N).
Challenge: Try doing it in O(1) space complexity.
Constraints:
1 ≤ length of a, b ≤ 105
0
aniket6518gadhe4 days ago
JAVA SOLUTION
Total Time Taken:
0.38/1.38
public static boolean isRotated(String str1, String str2)
    {
        String temp=str1;
        temp = temp.substring(str1.length()-2,str1.length()) + str1.substring(0,str1.length()-2);
        str1 = str1.substring(2,str1.length()) + str1.substring(0,2);
        if(str1.equals(str2) || temp.equals(str2))
            return true;
        return false;
    }
0
penchalaswamimannarapu2286 days ago
class Solution:   #Function to check if a string can be obtained by rotating   #another string by exactly 2 places.   def isRotated(self,str1,str2):       #code here       n=2       left=str1[0:n]+str1[n:]       right=str1[-n:]+str1[0:-n]       if left==str2 or right==str2:           return 1       else:           return 0
 
0
geminicode1 week ago
easy and precise python solution
def isRotated(self,str1,str2):
        #code here
        if str2[len(str2)-2:] + str2[:len(str2)-2] == str1:
            return 1
        elif str2[2:] + str2[:2] == str1:
            return 1
        return 0
0
codewithshoaib191 week ago
public static boolean isRotated(String str1, String str2)   {       // Your code here       if(str1.length()!=str2.length()){           return false;       }       String a=str1.substring(2) +str1.substring(0,2);      String b=str1.substring(str1.length()-2) + str1.substring(0,str1.length()-2);   //     System.out.println(a);   //   System.out.println(b);       if(str2.equals(a)){           return true;       }       else if(str2.equals(b)){           return true;       }        return false;   }
0
abhishekbandhu2 weeks ago
String right =str.substring(2)+str.substring(0,2);       String left=str.substring(str.length()-2) + str.substring(0,str.length()-2);       if(str2.equals(right) || str2.equals(left))       return true;       return false;
0
arkoprovodatta2 weeks ago
EASY SOLUTION WITH O(1) COMPLEXITY
class Solution{   public:   //Function to check if a string can be obtained by rotating   //another string by exactly 2 places.   bool isRotated(string str1, string str2)   {       string anti = str1.substr(2,str1.length()-2) + str1.substr(0,2);       string clk = str1.substr(str1.length()- 2, 2) + str1.substr(0,str1.length()- 2);       if(str2 == anti || str2 == clk){           return true;       }       else{           return false;       }          }
};
0
sanchitkhatar332 weeks ago
Simple and fast solution
TC = O(n)
SC = O(1)
bool isRotated(string s1, string s2)
    {
        // Your code here
        int n = s1.size();
        bool f1 = true,f2 = true;
        if(s1.size() != s2.size()) return false;
        for(int i = 2;i<=n-2;i++){
            //this loop will check for clockwise
            /*
                s1 = amazon
                       ^
                s2 = azonam
                     ^
            */
            if(s1[i] != s2[i-2]){f1 = false; break;}
        }
        for(int i = 2;i<=n-2;i++){
            //this loop will check for anti-clockwise remaining characters
            // if the remaining characters are same then f2 will be true
            /*
                s1 = amazon
                     ^
                s2 = onamaz
                       ^
            */
            if(s1[i-2] != s2[i]){f2 = false; break;}
        }
        if(f1 == false && f2 == false) return false;
        if((s1[0] == s2[s1.size()-2] && s1[1] == s2[s1.size()-1]) || (s2[0] == s1[s2.size()-2] && s2[1] == s1[s2.size()-1])) return true;
		return false;
    }
0
surajsingh30912 weeks ago
public static boolean isRotated(String str1, String str2)   {       String antitemp=str1.substring(0,2);       String finAnti=str1.substring(2);        String clocktemp=str1.substring(str1.length()-2);       String finclock=str1.substring(0,str1.length()-2);       if((finAnti+antitemp).equals(str2)){          return true;        }else if((clocktemp+finclock).equals(str2)){           return true;       }else{           return false;       }                     }
+3
rajatgupta96962 weeks ago
 bool isRotated(string str1, string str2)
    {
        // Your code here
        int n=str1.length();
        
        if(str2.length()!=n)
            return  false;
        if(n<=2 && str1 ==  str2)
            return true;
        string A = str1.substr(2)+str1.substr(0,2);
        string B = str1.substr(n-2,2)+str1.substr(0,n-2);
        if(A==str2 || B==str2)
            return true;
        else
            return false;
    }
0
tanishabhadanianve2 weeks ago
// JAVA SOLUTION IN O(1) SPACE
 
 public static boolean isRotated(String str1, String str2)   {       // Your code here          str1 = str1.substring(2) + str1.substring(0,2);       if(str1.equals(str2))           return true;                  str1 = str1.substring(str1.length()-4) +             str1.substring(0,str1.length()-4);              if(str1.equals(str2))           return true;                        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.