Given two strings, the task is to find if a string can be obtained by rotating another string two places.
Input: string1 = “amazon”, string2 = “azonam”
// rotated anti-clockwise
Input: string1 = “amazon”, string2 = “onamaz”
// 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.
Exercise : Check if string2 is obtained by rotating string1 by k places.
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 email@example.com. 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.
- 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
- 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
- Count occurrences of a string that can be constructed from another given string
- String obtained by reversing and complementing a Binary string K times