Search⌘ K
AI Features

Solution: Number of Ways to Form Target String Given a Dictionary

Explore how to use dynamic programming combined with frequency counting to find the total number of ways to form a target string from multiple dictionary words. Understand how to build a frequency table, implement a dp solution to track prefix formations, and optimize calculations to handle large inputs within modulo constraints.

Statement

You are given a list of nn strings, words, where each string has the same length mm, and a target string, target, of length tt. A target can be formed using the given words under the following rules:

  • You must build the target from left to right.

  • To form the ith character (0-indexed) of target, you can choose any kth character of any jth string in words, i.e., target[i] = words[j][k].

  • Once you use the kth character of the jth string in words, your next letter can't be a character from index 00 through k of any string in words. In simple words, all characters to the left of or at index k become unusable for every string, and the next letter must come from positions strictly to the right of k.

  • 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 109+710^{9} + 7.

Constraints:

  • 11 \leq words.length 1000\leq 1000

  • 11 \leq ...