Number of Ways to Form a Target String Given a Dictionary - Problem

You are given a list of strings words of the same length and a string target.

Your task is to form target using the given words under the following rules:

  • target should be formed from left to right.
  • To form the ith character (0-indexed) of target, you can choose the kth character of the jth string in words if target[i] = words[j][k].
  • Once you use the kth character of the jth string of words, you can no longer use the xth character of any string in words where x <= k. In other words, all characters to the left of or at index k become unusable for every string.
  • Repeat the process until you form the string target.

Notice that you can use multiple characters from the same string in words provided the conditions above are met.

Return the number of ways to form target from words. Since the answer may be too large, return it modulo 109 + 7.

Input & Output

Example 1 — Basic Case
$ Input: words = ["acca","bbbb","caca"], target = "aba"
Output: 6
💡 Note: 6 ways to form 'aba': use 'a' from position 0, 'b' from position 1, 'a' from position 2. Multiple combinations possible due to multiple words containing required characters.
Example 2 — No Solution
$ Input: words = ["abcd"], target = "abce"
Output: 0
💡 Note: Cannot form 'abce' because there's no 'e' in any word, so it's impossible to complete the target string.
Example 3 — Single Character
$ Input: words = ["abab","baba"], target = "a"
Output: 4
💡 Note: Can choose 'a' from position 0 or position 2, and from either word, giving us 2×2 = 4 ways total.

Constraints

  • 1 ≤ words.length ≤ 1000
  • 1 ≤ words[i].length ≤ 1000
  • All strings in words have the same length
  • 1 ≤ target.length ≤ 1000
  • words[i] and target contain only lowercase English letters

Visualization

Tap to expand
Number of Ways to Form Target String INPUT words array (3 strings): pos: 0 1 2 3 a c c a "acca" b b b b "bbbb" c a c a "caca" target = "aba" a b a Character counts by position: pos 0: a=1, b=1, c=1 pos 1: a=1, b=1, c=1 pos 2: a=0, b=1, c=2 pos 3: a=2, b=1, c=0 n=3 words, len=4, target len=3 ALGORITHM STEPS 1 Build frequency table Count chars at each position 2 Define DP state dp[i][j] = ways to form target[0..i] using pos 0..j 3 Recurrence relation dp[i][j] = dp[i][j-1] + dp[i-1][j-1] * count[j][char] 4 Fill DP table Iterate i=0..2, j=0..3 DP Table (simplified): j--> 0 1 2 3 a 1 2 2 4 b 0 1 3 4 a 0 0 0 6 Result! Time: O(m * n), Space: O(m * n) FINAL RESULT 6 Ways to Form "aba": 1. pos0[acca]=a, pos1[bbbb]=b, pos3[acca]=a 2. pos0[acca]=a, pos1[bbbb]=b, pos3[caca]=a 3. pos0[acca]=a, pos2[bbbb]=b, pos3[acca]=a 4. pos0[acca]=a, pos2[bbbb]=b, pos3[caca]=a 5. pos1[caca]=a, pos2[bbbb]=b, pos3[acca]=a 6. pos1[caca]=a, pos2[bbbb]=b, pos3[caca]=a Output: 6 OK - All 6 ways verified (Result mod 10^9 + 7) Key Insight: Precompute character frequencies at each position. Use 2D DP where dp[i][j] represents the number of ways to form target[0..i] using word positions 0..j. The recurrence adds: (1) skip position j, or (2) use position j if it has the required character, multiplying by the frequency count. TutorialsPoint - Number of Ways to Form a Target String Given a Dictionary | DP Approach
Asked in
Amazon 15 Google 12 Microsoft 8
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen