Skip to content

Commit 44bbd3a

Browse files
committed
[LeetCode Sync] Runtime - 1133 ms (64.56%), Memory - 23.4 MB (76.76%)
1 parent 95b0ccc commit 44bbd3a

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 &lt;= 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>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> words = [&quot;acca&quot;,&quot;bbbb&quot;,&quot;caca&quot;], target = &quot;aba&quot;
21+
<strong>Output:</strong> 6
22+
<strong>Explanation:</strong> There are 6 ways to form target.
23+
&quot;aba&quot; -&gt; index 0 (&quot;<u>a</u>cca&quot;), index 1 (&quot;b<u>b</u>bb&quot;), index 3 (&quot;cac<u>a</u>&quot;)
24+
&quot;aba&quot; -&gt; index 0 (&quot;<u>a</u>cca&quot;), index 2 (&quot;bb<u>b</u>b&quot;), index 3 (&quot;cac<u>a</u>&quot;)
25+
&quot;aba&quot; -&gt; index 0 (&quot;<u>a</u>cca&quot;), index 1 (&quot;b<u>b</u>bb&quot;), index 3 (&quot;acc<u>a</u>&quot;)
26+
&quot;aba&quot; -&gt; index 0 (&quot;<u>a</u>cca&quot;), index 2 (&quot;bb<u>b</u>b&quot;), index 3 (&quot;acc<u>a</u>&quot;)
27+
&quot;aba&quot; -&gt; index 1 (&quot;c<u>a</u>ca&quot;), index 2 (&quot;bb<u>b</u>b&quot;), index 3 (&quot;acc<u>a</u>&quot;)
28+
&quot;aba&quot; -&gt; index 1 (&quot;c<u>a</u>ca&quot;), index 2 (&quot;bb<u>b</u>b&quot;), index 3 (&quot;cac<u>a</u>&quot;)
29+
</pre>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> words = [&quot;abba&quot;,&quot;baab&quot;], target = &quot;bab&quot;
35+
<strong>Output:</strong> 4
36+
<strong>Explanation:</strong> There are 4 ways to form target.
37+
&quot;bab&quot; -&gt; index 0 (&quot;<u>b</u>aab&quot;), index 1 (&quot;b<u>a</u>ab&quot;), index 2 (&quot;ab<u>b</u>a&quot;)
38+
&quot;bab&quot; -&gt; index 0 (&quot;<u>b</u>aab&quot;), index 1 (&quot;b<u>a</u>ab&quot;), index 3 (&quot;baa<u>b</u>&quot;)
39+
&quot;bab&quot; -&gt; index 0 (&quot;<u>b</u>aab&quot;), index 2 (&quot;ba<u>a</u>b&quot;), index 3 (&quot;baa<u>b</u>&quot;)
40+
&quot;bab&quot; -&gt; index 1 (&quot;a<u>b</u>ba&quot;), index 2 (&quot;ba<u>a</u>b&quot;), index 3 (&quot;baa<u>b</u>&quot;)
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
48+
<li><code>1 &lt;= words[i].length &lt;= 1000</code></li>
49+
<li>All strings in <code>words</code> have the same length.</li>
50+
<li><code>1 &lt;= target.length &lt;= 1000</code></li>
51+
<li><code>words[i]</code> and <code>target</code> contain only lowercase English letters.</li>
52+
</ul>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def numWays(self, words: List[str], target: str) -> int:
3+
MOD = 1000000007
4+
word_length = len(words[0])
5+
target_length = len(target)
6+
char_frequency = [[0] * 26 for _ in range(word_length)]
7+
8+
# Step 1: Calculate frequency of each character at every index in "words".
9+
for word in words:
10+
for j in range(word_length):
11+
char_frequency[j][ord(word[j]) - ord("a")] += 1
12+
13+
# Step 2: Initialize two DP arrays: prev_count and curr_count.
14+
prev_count = [0] * (target_length + 1)
15+
curr_count = [0] * (target_length + 1)
16+
17+
# Base case: There is one way to form an empty target string.
18+
prev_count[0] = 1
19+
20+
# Step 3: Fill the DP arrays.
21+
for curr_word in range(1, word_length + 1):
22+
curr_count = prev_count.copy()
23+
for curr_target in range(1, target_length + 1):
24+
cur_pos = ord(target[curr_target - 1]) - ord("a")
25+
26+
# If characters match, add the number of ways.
27+
curr_count[curr_target] += (
28+
char_frequency[curr_word - 1][cur_pos]
29+
* prev_count[curr_target - 1]
30+
) % MOD
31+
curr_count[curr_target] %= MOD
32+
33+
# Move current row to previous row for the next iteration.
34+
prev_count = curr_count.copy()
35+
36+
# Step 4: The result is in prev[target_length].
37+
return curr_count[target_length]

0 commit comments

Comments
 (0)