Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right).
Input: str1 = “abcdefg”, str2 = “cdefgab”, d = 2
Rotate str1 2 places to the left.
Input: str1 = “abcdefg”, str2 = “cdfdawb”, d = 6
Approach: An approach to solve the same problem has been discussed here. In this article, reversal algorithm is used to rotate the string to the left and to the right in O(n). If any one of the rotations of str1 is equal to str2 then print Yes else print No.
Below is the implementation of the above approach:
Time Complexity: O(n)
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 2 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01