Skip to content

Commit 4112973

Browse files
committed
Sync LeetCode submission Runtime - 661 ms (24.05%), Memory - 21.2 MB (91.83%)
1 parent 49d0860 commit 4112973

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<p>Given two strings <code>str1</code> and <code>str2</code>, return <em>the shortest string that has both </em><code>str1</code><em> and </em><code>str2</code><em> as <strong>subsequences</strong></em>. If there are multiple valid strings, return <strong>any</strong> of them.</p>
2+
3+
<p>A string <code>s</code> is a <strong>subsequence</strong> of string <code>t</code> if deleting some number of characters from <code>t</code> (possibly <code>0</code>) results in the string <code>s</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> str1 = &quot;abac&quot;, str2 = &quot;cab&quot;
10+
<strong>Output:</strong> &quot;cabac&quot;
11+
<strong>Explanation:</strong>
12+
str1 = &quot;abac&quot; is a subsequence of &quot;cabac&quot; because we can delete the first &quot;c&quot;.
13+
str2 = &quot;cab&quot; is a subsequence of &quot;cabac&quot; because we can delete the last &quot;ac&quot;.
14+
The answer provided is the shortest such string that satisfies these properties.
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> str1 = &quot;aaaaaaaa&quot;, str2 = &quot;aaaaaaaa&quot;
21+
<strong>Output:</strong> &quot;aaaaaaaa&quot;
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= str1.length, str2.length &lt;= 1000</code></li>
29+
<li><code>str1</code> and <code>str2</code> consist of lowercase English letters.</li>
30+
</ul>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Approach 3: Bottom-Up Dynamic Programming
2+
3+
# n = len(str1), m = len(str2)
4+
# Time: O(n * m * (n + m))
5+
# Space: O(m * (n + m))
6+
7+
class Solution:
8+
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
9+
str1_len = len(str1)
10+
str2_len = len(str2)
11+
12+
# Initialize the first row (when str1 is empty, the supersequence is str2's prefix)
13+
prev_row = [str2[:col] for col in range(str2_len + 1)]
14+
15+
for row in range(1, str1_len + 1):
16+
# Initialize the first column (when str2 is empty, the supersequence is str1's prefix)
17+
curr_row = [str1[:row]] + [None for _ in range(str2_len)]
18+
19+
for col in range(1, str2_len + 1):
20+
# If characters match, extend the supersequence from the diagonal value
21+
if str1[row - 1] == str2[col - 1]:
22+
curr_row[col] = prev_row[col - 1] + str1[row - 1]
23+
else:
24+
# If characters do not match, choose the shorter supersequence
25+
# From previous row (exclude current str1 char)
26+
pick_s1 = prev_row[col]
27+
# From previous column (exclude current str2 char)
28+
pick_s2 = curr_row[col - 1]
29+
30+
curr_row[col] = (pick_s1 + str1[row - 1] if len(pick_s1) < len(pick_s2)
31+
else pick_s2 + str2[col - 1])
32+
33+
# Move to next row
34+
prev_row = curr_row
35+
36+
return prev_row[str2_len]

0 commit comments

Comments
 (0)