java.lang.Object
g1601_1700.s1639_number_of_ways_to_form_a_target_string_given_a_dictionary.Solution

public class Solution extends Object
1639 - Number of Ways to Form a Target String Given a Dictionary.<p>Hard</p> <p>You are given a list of strings of the <strong>same length</strong> <code>words</code> and a string <code>target</code>.</p> <p>Your task is to form <code>target</code> using the given <code>words</code> under the following rules:</p> <ul> <li><code>target</code> should be formed from left to right.</li> <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> <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> <li>Repeat the process until you form the string <code>target</code>.</li> </ul> <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> <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> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;acca&rdquo;,&ldquo;bbbb&rdquo;,&ldquo;caca&rdquo;], target = &ldquo;aba&rdquo;</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> There are 6 ways to form target.</p> <p>&ldquo;aba&rdquo; -> index 0 (&ldquo;acca&rdquo;), index 1 (&ldquo;bbbb&rdquo;), index 3 (&ldquo;caca&rdquo;)</p> <p>&ldquo;aba&rdquo; -> index 0 (&ldquo;acca&rdquo;), index 2 (&ldquo;bbbb&rdquo;), index 3 (&ldquo;caca&rdquo;)</p> <p>&ldquo;aba&rdquo; -> index 0 (&ldquo;acca&rdquo;), index 1 (&ldquo;bbbb&rdquo;), index 3 (&ldquo;acca&rdquo;)</p> <p>&ldquo;aba&rdquo; -> index 0 (&ldquo;acca&rdquo;), index 2 (&ldquo;bbbb&rdquo;), index 3 (&ldquo;acca&rdquo;)</p> <p>&ldquo;aba&rdquo; -> index 1 (&ldquo;caca&rdquo;), index 2 (&ldquo;bbbb&rdquo;), index 3 (&ldquo;acca&rdquo;)</p> <p>&ldquo;aba&rdquo; -> index 1 (&ldquo;caca&rdquo;), index 2 (&ldquo;bbbb&rdquo;), index 3 (&ldquo;caca&rdquo;)</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;abba&rdquo;,&ldquo;baab&rdquo;], target = &ldquo;bab&rdquo;</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> There are 4 ways to form target.</p> <p>&ldquo;bab&rdquo; -> index 0 (&ldquo;baab&rdquo;), index 1 (&ldquo;baab&rdquo;), index 2 (&ldquo;abba&rdquo;)</p> <p>&ldquo;bab&rdquo; -> index 0 (&ldquo;baab&rdquo;), index 1 (&ldquo;baab&rdquo;), index 3 (&ldquo;baab&rdquo;)</p> <p>&ldquo;bab&rdquo; -> index 0 (&ldquo;baab&rdquo;), index 2 (&ldquo;baab&rdquo;), index 3 (&ldquo;baab&rdquo;)</p> <p>&ldquo;bab&rdquo; -> index 1 (&ldquo;abba&rdquo;), index 2 (&ldquo;baab&rdquo;), index 3 (&ldquo;baab&rdquo;)</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 1000</code></li> <li><code>1 <= words[i].length <= 1000</code></li> <li>All strings in <code>words</code> have the same length.</li> <li><code>1 <= target.length <= 1000</code></li> <li><code>words[i]</code> and <code>target</code> contain only lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numWays

      public int numWays(String[] words, String target)