Given an array of strings A, each of the same length and a target string S, find the number of ways to construct the target string using characters from strings in the given array such that the indices of the characters used in string construction form a strictly increasing sequence. Multiple characters can also be used from the same string. Since the answer can be very large, take modulo with (10^9 + 7)
Input : A = ["adc", "aec", "erg"], S = "ac" Output : 4 Target string can be formed in following ways : 1) 1st character of "adc" and the 3rd character of "adc". 2) 1st character of "adc" and the 3rd character of "aec". 3) 1st character of "aec" and the 3rd character of "adc". 4) 1st character of "aec" and the 3rd character of "aec". Input : A = ["afsdc", "aeeeedc", "ddegerg"], S = "ae" Output : 12
- We will use Dynamic Programming to solve the problem.
- For each string in the array, store the positions in which characters occurred in the string in a common list (L). Our aim is to use the characters whose indices form a strictly increasing sequence, so it doesn’t matter which string they come from.
- Traverse the target string, and keep the information of previously picked index (prev). For the current position of the target string check whether this character has occurred at any index greater than prev by searching in L. This can be done naively using recursion but we can memoize it using dp table.
Following are the states of dp : dp[pos][prev], where pos represents the position we are at in the target string, and prev represents the previously picked index.
Time complexity : O(M * N2), where M is the length of target string, and N is the length of each of the array strings.
- Count possible ways to construct buildings
- Number of ways to calculate a target number using only array elements
- Count number of ways to convert string S to T by performing K cyclic shifts
- Count of ways to split given string into two non-empty palindromes
- Count ways to partition a string such that both parts have equal distinct characters
- Count number of ways to get Odd Sum
- Count number of ways to divide a number in 4 parts
- Count number of ways to arrange first N numbers
- Count the number of ways to traverse a Matrix
- Count number of ways to cover a distance
- Count number of ways to partition a set into k subsets
- Count number of ways to jump to reach end
- Count number of ways to cover a distance | Set 2
- Count ways to express a number as sum of powers
- Count number of ways to reach destination in a Maze using BFS
- Count number of ways to fill a "n x 4" grid using "1 x 4" tiles
- Count number of ways to reach destination in a maze
- Count number of ways to reach destination in a Maze
- Count of ways to represent N as sum of a prime number and twice of a square
- Count number of ways to reach a given score in a Matrix
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.