Given two sequences A, B, find out number of unique ways in sequence A, to form a subsequence of A that is identical to the sequence B. Transformation is meant by converting string A (by removing 0 or more characters) to string B.
Input : A = "abcccdf", B = "abccdf" Output : 3 Explanation : Three ways will be -> "ab.ccdf", "abc.cdf" & "abcc.df" . "." is where character is removed. Input : A = "aabba", B = "ab" Output : 4 Expalnation : Four ways will be -> "a.b..", "a..b.", ".ab.." & ".a.b." . "." is where characters are removed.
Asked in : Google
The idea to solve this problem is using Dynamic Programming. Construct a 2D DP matrix of m*n size, where m is size of string B and n is size of string A.
dp[i][j] gives the number of ways of transforming string A[0…j] to B[0…i].
- Case 1 : dp[j] = 1, since placing B = “” with any substring of A would have only 1 solution which is to delete all characters in A.
- Case 2 : when i > 0, dp[i][j] can be derived by two cases:
- Case 2.a : if B[i] != A[j], then the solution would be to ignore the character A[j] and align substring B[0..i] with A[0..(j-1)]. Therefore, dp[i][j] = dp[i][j-1].
- Case 2.b : if B[i] == A[j], then first we could have the solution in case a), but also we could match the characters B[i] and A[j] and place the rest of them (i.e. B[0..(i-1)] and A[0..(j-1)]. As a result, dp[i][j] = dp[i][j-1] + dp[i-1][j-1].
Time Complexity: O(n^2)
This article is contributed by Jatin Goyal. 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 characters of one string can be swapped to form other
- Check if end of a sorted Array can be reached by repeated jumps of one more, one less or same number of indices as previous jump
- Maximum non-repeating characters after removing K characters
- Sub-strings that start and end with one character and have at least one other
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Check if a String contains any index with more than K active characters
- Minimize the length of string by removing occurrence of only one character
- Lexicographically smallest string formed by removing at most one character
- Reduce the string by removing K consecutive identical characters
- Check if string remains palindrome after removing given number of characters
- Minimize cost to empty a given string by removing characters alphabetically
- Check if permutation of a given string can be made palindromic by removing at most K characters
- Minimum sum of squares of character counts in a given string after removing k characters
- Minimum length of Run Length Encoding possible by removing at most K characters from a given string
- Minimize a binary string by repeatedly removing even length substrings of same characters
- Ways to write n as sum of two or more positive integers
- Ways to write N as sum of two or more positive integers | Set-2
- Print all possible ways to convert one string into another string | Edit-Distance
- Find indices of all occurrence of one string in other
- Check if max occurring character of one string appears same no. of times in other