Solving Leetcode Interviews in Seconds with AI: Shortest Common Supersequence
Introduction
In this blog post, we will explore how to solve the LeetCode problem "1092" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them. A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s. Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties. Example 2: Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa" Output: "aaaaaaaa" Constraints: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
Explanation
Here's the breakdown of the approach, complexities, and the Python code:
High-Level Approach:
- Compute the Longest Common Subsequence (LCS) of the two input strings.
- Build the shortest common supersequence (SCS) by merging the two strings, taking the LCS into account to avoid duplication. Characters in the LCS appear only once in the SCS.
- Iterate through both strings and if a character is part of the LCS include it only once, otherwise include the character from either
str1orstr2
Complexity:
- Runtime: O(m*n), where m and n are the lengths of
str1andstr2respectively. Storage: O(m*n) due to the DP table used for LCS.
- Runtime: O(m*n), where m and n are the lengths of
Code
def shortestCommonSupersequence(str1: str, str2: str) -> str:
"""
Finds the shortest common supersequence of two strings.
Args:
str1: The first string.
str2: The second string.
Returns:
The shortest common supersequence.
"""
n = len(str1)
m = len(str2)
# Compute the length of the LCS using dynamic programming
dp = [["" for _ in range(m + 1)] for _ in range(n + 1)]
for i in range(n + 1):
for j in range(m + 1):
if i == 0:
dp[i][j] = str2[:j]
elif j == 0:
dp[i][j] = str1[:i]
elif str1[i - 1] == str2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + str1[i - 1]
else:
if len(dp[i - 1][j]) < len(dp[i][j - 1]):
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = dp[i][j-1]
# Compute the LCS length. We need to find lcs, not its length, so we use the dp table from above
# Construct the SCS using the LCS information
i = n
j = m
scs = ""
while i > 0 and j > 0:
if str1[i - 1] == str2[j - 1]:
scs = str1[i - 1] + scs
i -= 1
j -= 1
else:
if dp[i-1][j] > dp[i][j-1]:
scs = str2[j-1] + scs
j -= 1
else:
scs = str1[i-1] + scs
i -= 1
# Add any remaining characters from str1 or str2
scs = str1[:i] + scs
scs = str2[:j] + scs
return scs