Skip to content

Commit 8b47601

Browse files
committed
Sync LeetCode submission Runtime - 858 ms (52.27%), Memory - 18 MB (93.18%)
1 parent 2887cf4 commit 8b47601

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<p>You are given a string array <code>words</code>, and an array <code>groups</code>, both arrays having length <code>n</code>.</p>
2+
3+
<p>The <strong>hamming distance</strong> between two strings of equal length is the number of positions at which the corresponding characters are <strong>different</strong>.</p>
4+
5+
<p>You need to select the <strong>longest</strong> <span data-keyword="subsequence-array">subsequence</span> from an array of indices <code>[0, 1, ..., n - 1]</code>, such that for the subsequence denoted as <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k-1</sub>]</code> having length <code>k</code>, the following holds:</p>
6+
7+
<ul>
8+
<li>For <strong>adjacent</strong> indices in the subsequence, their corresponding groups are <strong>unequal</strong>, i.e., <code>groups[i<sub>j</sub>] != groups[i<sub>j+1</sub>]</code>, for each <code>j</code> where <code>0 &lt; j + 1 &lt; k</code>.</li>
9+
<li><code>words[i<sub>j</sub>]</code> and <code>words[i<sub>j+1</sub>]</code> are <strong>equal</strong> in length, and the <strong>hamming distance</strong> between them is <code>1</code>, where <code>0 &lt; j + 1 &lt; k</code>, for all indices in the subsequence.</li>
10+
</ul>
11+
12+
<p>Return <em>a string array containing the words corresponding to the indices <strong>(in order)</strong> in the selected subsequence</em>. If there are multiple answers, return <em>any of them</em>.</p>
13+
14+
<p><strong>Note:</strong> strings in <code>words</code> may be <strong>unequal</strong> in length.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
20+
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">words = [&quot;bab&quot;,&quot;dab&quot;,&quot;cab&quot;], groups = [1,2,2]</span></p>
21+
22+
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">[&quot;bab&quot;,&quot;cab&quot;]</span></p>
23+
24+
<p><strong>Explanation: </strong>A subsequence that can be selected is <code>[0,2]</code>.</p>
25+
26+
<ul>
27+
<li><code>groups[0] != groups[2]</code></li>
28+
<li><code>words[0].length == words[2].length</code>, and the hamming distance between them is 1.</li>
29+
</ul>
30+
31+
<p>So, a valid answer is <code>[words[0],words[2]] = [&quot;bab&quot;,&quot;cab&quot;]</code>.</p>
32+
33+
<p>Another subsequence that can be selected is <code>[0,1]</code>.</p>
34+
35+
<ul>
36+
<li><code>groups[0] != groups[1]</code></li>
37+
<li><code>words[0].length == words[1].length</code>, and the hamming distance between them is <code>1</code>.</li>
38+
</ul>
39+
40+
<p>So, another valid answer is <code>[words[0],words[1]] = [&quot;bab&quot;,&quot;dab&quot;]</code>.</p>
41+
42+
<p>It can be shown that the length of the longest subsequence of indices that satisfies the conditions is <code>2</code>.</p>
43+
</div>
44+
45+
<p><strong class="example">Example 2:</strong></p>
46+
47+
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
48+
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">words = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;], groups = [1,2,3,4]</span></p>
49+
50+
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;]</span></p>
51+
52+
<p><strong>Explanation: </strong>We can select the subsequence <code>[0,1,2,3]</code>.</p>
53+
54+
<p>It satisfies both conditions.</p>
55+
56+
<p>Hence, the answer is <code>[words[0],words[1],words[2],words[3]] = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;]</code>.</p>
57+
58+
<p>It has the longest length among all subsequences of indices that satisfy the conditions.</p>
59+
60+
<p>Hence, it is the only answer.</p>
61+
</div>
62+
63+
<p>&nbsp;</p>
64+
<p><strong>Constraints:</strong></p>
65+
66+
<ul>
67+
<li><code>1 &lt;= n == words.length == groups.length &lt;= 1000</code></li>
68+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
69+
<li><code>1 &lt;= groups[i] &lt;= n</code></li>
70+
<li><code>words</code> consists of <strong>distinct</strong> strings.</li>
71+
<li><code>words[i]</code> consists of lowercase English letters.</li>
72+
</ul>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Approach: Dynamic Programming
2+
3+
class Solution:
4+
def getWordsInLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
5+
n = len(groups)
6+
dp = [1] * n
7+
prev = [-1] * n
8+
max_idx = 0
9+
10+
for i in range(1, n):
11+
for j in range(i):
12+
if(
13+
self.check(words[i], words[j])
14+
and dp[j] + 1 > dp[i]
15+
and groups[i] != groups[j]
16+
):
17+
dp[i] = dp[j] + 1
18+
prev[i] = j
19+
20+
if dp[i] > dp[max_idx]:
21+
max_idx = i
22+
23+
ans = []
24+
i = max_idx
25+
26+
while i >= 0:
27+
ans.append(words[i])
28+
i = prev[i]
29+
ans.reverse()
30+
return ans
31+
32+
def check(self, s1, s2):
33+
if len(s1) != len(s2):
34+
return False
35+
diff = 0
36+
for c1, c2 in zip(s1, s2):
37+
if c1 != c2:
38+
diff += 1
39+
if diff > 1:
40+
return False
41+
42+
return diff == 1
43+

0 commit comments

Comments
 (0)