Given two strings S and T and a number K, the task is to count the number of ways to convert string S to string T by performing K cyclic shifts.
The cyclic shift is defined as the string S can be split into two non-empty parts X + Y and in one operation we can transform S to Y + X from X + Y.
Note: Since count can be very large print the answer to modulo 109 + 7.
Input: S = “ab”, T = “ab”, K = 2
The only way to do this is to convert [ab to ba] in the first move and then [ba to ab] in the second move.
Input: S = “ababab”, T = “ababab”, K = 1
One possible way to convert S to T in one move is [ab | abab] -> [ababab], the second way is [abab | ab] -> [ababab]. So there are total two ways.
Approach: This problem can be solved using Dynamic Programming. Let us call a cyclic shift ‘good’ if at the end we are at string T and the vice versa for ‘bad’. Below are the steps:
- Precompute the number of good(denoted by a) and bad(denoted by b) cyclic shifts.
- Initialize two dp arrays such that dp1[i] denote the number of ways to get to a good shift in i moves and dp2[i] denotes the number of ways to get to a bad shift in i moves.
- For transition, we are only concerned about previous state i.e., (i – 1)th state and the answer to this question is dp1[K].
- So the number of ways to reach a good state in i moves is equal to the number of ways of reaching a good shift in i-1 moves multiplied by (a-1) (as last shift is also good)
- So the number of ways of reaching a bad shift in i-1 moves multiplied by (a)(as next move can be any of the good shifts).
Below is the recurrence relation for the good and bad shifts:
So for good shifts we have:
Similarly, for bad shifts we have:
Below is the implementation of above approach:
Time Complexity: O(N)
Auxiliary Space: O(K)
- Sort a string lexicographically using triple cyclic shifts
- Cyclic shifts of integer N by another integer m
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Number of Counterclockwise shifts to make a string palindrome
- Count of ways to travel a cyclic path in N steps in a Triangular Pyramid
- Count of right shifts for each array element to be in its sorted position
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Count of cyclic permutations having XOR with other binary string as 0
- Count all indices of cyclic regular parenthesis
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Minimum moves to reach from i to j in a cyclic string
- Number of cyclic elements in an array where we can jump according to value
- Number of different cyclic paths of length N in a tetrahedron
- Print all possible ways to convert one string into another string | Edit-Distance
- Number of ways to convert a character X to a string Y
- Cyclic Iterator for K variable length vectors
- Check if a decreasing Array can be sorted using Triple cyclic shift
- Sort permutation of N natural numbers using triple cyclic right swaps
- Find initial sequence that produces a given Array by cyclic increments upto index P
- Count ways to split a Binary String into three substrings having equal count of zeros
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.