Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times.
Input: S1 = “abcd”, S2 = “dddd”, X = 3
Given string S1 can be converted to string S2 as-
Character “a” – Shift 3 times – “d”
Character “b” – Shift 2 times – “d”
Character “c” – Shift 1 times – “d”
Character “d” – Shift 0 times – “d”
Input: S1 = “you”, S2 = “ara”, X = 6
Given string S1 can be converted to string S2 as –
Character “y” – Circular Shift 2 times – “a”
Character “o” – Shift 3 times – “r”
Character “u” – Circular Shift 6 times – “a”
Approach: The idea is to traverse the string and for each index and find the difference between the ASCII values of the character at the respective indices of the two strings. If the difference is less than 0, then for a circular shift, add 26 to get the actual difference. If for any index, the difference exceeds X, then S2 can’t be formed from S1, otherwise possible.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check whether second string can be formed from characters of first string
- Check if given string can be formed by two other strings or their permutations
- Check if K palindromic strings can be formed from a given string
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Number of Counterclockwise shifts to make a string palindrome
- Sort a string lexicographically using triple cyclic shifts
- Count number of ways to convert string S to T by performing K cyclic shifts
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Count of times second string can be formed from the characters of first string
- Check if the given string of words can be formed from words present in the dictionary
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be obtained by rotating another string 2 places
- Queries to check if string B exists as substring in string A
- 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
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 : Ganeshchowdharysadanala