Skip to content

Commit 7f8db2b

Browse files
committed
Feb 28
1 parent 487de03 commit 7f8db2b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

2025-02-February-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
| February 25 | [1524. Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | Medium | Solved |
3232
| February 26 | [1749. Maximum Absolute Sum of Any Subarray](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/) | Medium | Unsolved |
3333
| February 27 | [873. Length of Longest Fibonacci Subsequence](https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/) | Medium | Solved |
34-
| February 28 | []() | | |
34+
| February 28 | [](https://leetcode.com/problems/shortest-common-supersequence/) | Hard | Unsolved |
3535

3636

3737
## Summary
3838
| Level | Problems | Solved | Unsolved |
3939
| --- | --- | --- | --- |
4040
| Easy | 6 | 6 | 0 |
4141
| Medium | 19 | 14 | 5 |
42-
| Hard | 1 | 1 | 0 |
42+
| Hard | 2 | 1 | 1 |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
3+
l1, l2 = len(str1), len(str2)
4+
prev_row = [str2[:col] for col in range(l2+1)]
5+
for row in range(1, l1+1):
6+
curr_row = [str1[:row]] + ([None]*l2)
7+
for col in range(1, l2+1):
8+
if str1[row-1] == str2[col-1]:
9+
curr_row[col] = prev_row[col-1] + str1[row-1]
10+
else:
11+
p_s1 = prev_row[col]
12+
p_s2 = curr_row[col-1]
13+
curr_row[col] = (p_s1 + str1[row-1]
14+
if len(p_s1) < len(p_s2)
15+
else p_s2 + str2[col-1])
16+
prev_row = curr_row
17+
return prev_row[l2]
18+
19+
20+
def main():
21+
str1 = 'abac'
22+
str2 = 'cab'
23+
assert Solution().shortestCommonSupersequence(str1, str2) == 'cabac'
24+
25+
str1 = 'bbbaaaba'
26+
str2 = 'bbababbb'
27+
assert Solution().shortestCommonSupersequence(str1, str2) == 'bbbaaababbb'
28+
29+
30+
if __name__ == '__main__':
31+
main()

0 commit comments

Comments
 (0)