|
| 1 | +<p>You are given a list of strings of the <strong>same length</strong> <code>words</code> and a string <code>target</code>.</p> |
| 2 | + |
| 3 | +<p>Your task is to form <code>target</code> using the given <code>words</code> under the following rules:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>target</code> should be formed from left to right.</li> |
| 7 | + <li>To form the <code>i<sup>th</sup></code> character (<strong>0-indexed</strong>) of <code>target</code>, you can choose the <code>k<sup>th</sup></code> character of the <code>j<sup>th</sup></code> string in <code>words</code> if <code>target[i] = words[j][k]</code>.</li> |
| 8 | + <li>Once you use the <code>k<sup>th</sup></code> character of the <code>j<sup>th</sup></code> string of <code>words</code>, you <strong>can no longer</strong> use the <code>x<sup>th</sup></code> character of any string in <code>words</code> where <code>x <= k</code>. In other words, all characters to the left of or at index <code>k</code> become unusuable for every string.</li> |
| 9 | + <li>Repeat the process until you form the string <code>target</code>.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p><strong>Notice</strong> that you can use <strong>multiple characters</strong> from the <strong>same string</strong> in <code>words</code> provided the conditions above are met.</p> |
| 13 | + |
| 14 | +<p>Return <em>the number of ways to form <code>target</code> from <code>words</code></em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong></p> |
| 18 | + |
| 19 | +<pre> |
| 20 | +<strong>Input:</strong> words = ["acca","bbbb","caca"], target = "aba" |
| 21 | +<strong>Output:</strong> 6 |
| 22 | +<strong>Explanation:</strong> There are 6 ways to form target. |
| 23 | +"aba" -> index 0 ("<u>a</u>cca"), index 1 ("b<u>b</u>bb"), index 3 ("cac<u>a</u>") |
| 24 | +"aba" -> index 0 ("<u>a</u>cca"), index 2 ("bb<u>b</u>b"), index 3 ("cac<u>a</u>") |
| 25 | +"aba" -> index 0 ("<u>a</u>cca"), index 1 ("b<u>b</u>bb"), index 3 ("acc<u>a</u>") |
| 26 | +"aba" -> index 0 ("<u>a</u>cca"), index 2 ("bb<u>b</u>b"), index 3 ("acc<u>a</u>") |
| 27 | +"aba" -> index 1 ("c<u>a</u>ca"), index 2 ("bb<u>b</u>b"), index 3 ("acc<u>a</u>") |
| 28 | +"aba" -> index 1 ("c<u>a</u>ca"), index 2 ("bb<u>b</u>b"), index 3 ("cac<u>a</u>") |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p><strong class="example">Example 2:</strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<strong>Input:</strong> words = ["abba","baab"], target = "bab" |
| 35 | +<strong>Output:</strong> 4 |
| 36 | +<strong>Explanation:</strong> There are 4 ways to form target. |
| 37 | +"bab" -> index 0 ("<u>b</u>aab"), index 1 ("b<u>a</u>ab"), index 2 ("ab<u>b</u>a") |
| 38 | +"bab" -> index 0 ("<u>b</u>aab"), index 1 ("b<u>a</u>ab"), index 3 ("baa<u>b</u>") |
| 39 | +"bab" -> index 0 ("<u>b</u>aab"), index 2 ("ba<u>a</u>b"), index 3 ("baa<u>b</u>") |
| 40 | +"bab" -> index 1 ("a<u>b</u>ba"), index 2 ("ba<u>a</u>b"), index 3 ("baa<u>b</u>") |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>1 <= words.length <= 1000</code></li> |
| 48 | + <li><code>1 <= words[i].length <= 1000</code></li> |
| 49 | + <li>All strings in <code>words</code> have the same length.</li> |
| 50 | + <li><code>1 <= target.length <= 1000</code></li> |
| 51 | + <li><code>words[i]</code> and <code>target</code> contain only lowercase English letters.</li> |
| 52 | +</ul> |
0 commit comments