Number of Ways to Form Target String Given a Dictionary
Try to solve the Number of Ways to Form Target String Given a Dictionary problem.
We'll cover the following
Statement
You are given a list of words
, where each string has the same length target
, of length target
can be formed using the given words
under the following rules:
You must build the
target
from left to right.To form the
i
th
character (0-indexed) oftarget
, you can choose anyk
th
character of anyj
th
string inwords
, i.e.,target[i] = words[j][k]
.Once you use the
k
th
character of thej
th
string inwords
, your next letter can't be a character from indexthrough k
of any string inwords
. In simple words, all characters to the left of or at indexk
become unusable for every string, and the next letter must come from positions strictly to the right ofk
.Repeat the process until you’ve formed the complete
target
.
Note: You can use multiple characters from the same string in
words
, as long as the rules above are followed.
Your task is to find and return the number of ways to form the string target
from words
. As the answer might be too large, return it modulo
Constraints:
words.length
words[i].length
All strings in
words
have the same length.target.length
words[i]
andtarget
contain only lowercase English letters.
Examples
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.