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
- String obtained by reversing and complementing a Binary string K times
- Lexicographically smallest string obtained after concatenating array
- Find i'th Index character in a binary string obtained after n iterations
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if given string is a substring of string formed by repeated concatenation of z to a
- 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
- Check if a string can become empty by recursively deleting a given sub-string
- Check if left and right shift of any string results into given string
- Queries to check if string B exists as substring in string A
- Check if any permutation of string is a K times repeated string
- Check if a string contains a palindromic sub-string of even length
- Check whether second string can be formed from characters of first string
- Check if a string is a scrambled form of another string
- Check if a string can be repeated to make another string
- Check if a given string is a Reverse Bitonic String or not
- Check if the given string is shuffled substring of another string
- Check if it is possible to sort the array after rotating it
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