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.
- Number of ways to calculate a target number using only array elements
- Count possible ways to construct buildings
- Construct a string that has exactly K subsequences from given string
- Count ways to split a Binary String into three substrings having equal count of zeros
- Print all possible expressions that evaluate to a target
- Minimum steps to reach target by a Knight | Set 2
- Probability of A winning the match when individual probabilities of hitting the target given
- Burn the binary tree starting from the target node
- Print all possible shortest chains to reach a target word
- Count number of ways to convert string S to T by performing K cyclic shifts
- Count of suffix increment/decrement operations to construct a given array
- Construct Binary Tree from String with bracket representation
- Minimum cost to construct a string
- Construct a binary string following the given constraints
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Construct the Cypher string based on the given conditions
- Count number of ways to divide a number in 4 parts
- Count ways to partition a string such that both parts have equal distinct characters
- Count of ways to split given string into two non-empty palindromes
- Print all possible ways to convert one string into another string | Edit-Distance
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.