Given two strings, the task is to find if a string can be obtained by rotating another string two places.
Examples:
Input: string1 = “amazon”, string2 = “azonam”
Output: Yes
// rotated anti-clockwiseInput: string1 = “amazon”, string2 = “onamaz”
Output: Yes
// rotated clockwise
Asked in: Amazon Interview
1- There can be only two cases:
a) Clockwise rotated
b) Anti-clockwise rotated
2- If clockwise rotated that means elements
are shifted in right.
So, check if a substring[2.... len-1] of
string2 when concatenated with substring[0,1]
of string2 is equal to string1. Then, return true.
3- Else, check if it is rotated anti-clockwise
that means elements are shifted to left.
So, check if concatenation of substring[len-2, len-1]
with substring[0....len-3] makes it equals to
string1. Then return true.
4- Else, return false.
Below is the implementation of above approach.
C++
// C++ program to check if a string is two time // rotation of another string. #include<bits/stdc++.h> using namespace std; // Function to check if string2 is obtained by // string 1 bool isRotated(string str1, string str2) { if (str1.length() != str2.length()) return false; string clock_rot = ""; string anticlock_rot = ""; int len = str2.length(); // Initialize string as anti-clockwise rotation anticlock_rot = anticlock_rot + str2.substr(len-2, 2) + str2.substr(0, len-2) ; // Initialize string as clock wise rotation clock_rot = clock_rot + str2.substr(2) + str2.substr(0, 2) ; // check if any of them is equal to string1 return (str1.compare(clock_rot) == 0 || str1.compare(anticlock_rot) == 0); } // Driver code int main() { string str1 = "geeks"; string str2 = "eksge"; isRotated(str1, str2) ? cout << "Yes" : cout << "No"; return 0; } |
Java
// Java program to check if a string is two time // rotation of another string. class Test { // Method to check if string2 is obtained by // string 1 static boolean isRotated(String str1, String str2) { if (str1.length() != str2.length()) return false; String clock_rot = ""; String anticlock_rot = ""; int len = str2.length(); // Initialize string as anti-clockwise rotation anticlock_rot = anticlock_rot + str2.substring(len-2, len) + str2.substring(0, len-2) ; // Initialize string as clock wise rotation clock_rot = clock_rot + str2.substring(2) + str2.substring(0, 2) ; // check if any of them is equal to string1 return (str1.equals(clock_rot) || str1.equals(anticlock_rot)); } // Driver method public static void main(String[] args) { String str1 = "geeks"; String str2 = "eksge"; System.out.println(isRotated(str1, str2) ? "Yes" : "No"); } } |
Python 3
# Python 3 program to check if a string # is two time rotation of another string. # Function to check if string2 is # obtained by string 1 def isRotated(str1, str2): if (len(str1) != len(str2)): return False clock_rot = "" anticlock_rot = "" l = len(str2) # Initialize string as anti-clockwise rotation anticlock_rot = (anticlock_rot + str2[l - 2:] + str2[0: l - 2]) # Initialize string as clock wise rotation clock_rot = clock_rot + str2[2:] + str2[0:2] # check if any of them is equal to string1 return (str1 == clock_rot or str1 == anticlock_rot) # Driver code if __name__ == "__main__": str1 = "geeks" str2 = "eksge"if isRotated(str1, str2): print("Yes") else: print("No") # This code is contributed by ita_c |
C#
using System; // c# program to check if a string is two time // rotation of another string. public class Test { // Method to check if string2 is obtained by // string 1 public static bool isRotated(string str1, string str2) { if (str1.Length != str2.Length) { return false; } string clock_rot = ""; string anticlock_rot = ""; int len = str2.Length; // Initialize string as anti-clockwise rotation anticlock_rot = anticlock_rot + str2.Substring(len - 2, len - (len - 2)) + str2.Substring(0, len - 2); // Initialize string as clock wise rotation clock_rot = clock_rot + str2.Substring(2) + str2.Substring(0, 2); // check if any of them is equal to string1 return (str1.Equals(clock_rot) || str1.Equals(anticlock_rot)); } // Driver method public static void Main(string[] args) { string str1 = "geeks"; string str2 = "eksge"; Console.WriteLine(isRotated(str1, str2) ? "Yes" : "No"); } } // This code is contributed by Shrikant13 |
Output:
Yes
Exercise : Check if string2 is obtained by rotating string1 by k places.
Reference : https://www.careercup.com/question?id=5734821229756416
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if a string can be obtained by rotating another string d places
- Check if a string can be obtained by appending subsequences of another string
- Check if string S2 can be obtained by appending subsequences of string S1
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Check if it is possible to sort the array after rotating it
- Check if it is possible to make array increasing or decreasing by rotating the array
- Check if a string can be repeated to make another string
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Queries to find maximum sum contiguous subarrays of given length in a rotating array
- Maximize sum of diagonal of a matrix by rotating all rows or all columns
- Check if one string can be converted to another
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Check if a string can be transformed to another by sorting substrings
- Check if permutaion of one string can break permutation of another
- Check if a given string can be converted to another by given possible swaps
- Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1s
- Count occurrences of a string that can be constructed from another given string
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Arrangement of the characters of a word such that all vowels are at odd places

