Practice
1. Consider the following Python code implementing space-optimized DP for regex matching. What is the final return value when calling isMatch('aab', 'c*a*b')?
def isMatch(s: str, p: str) -> bool:
m, n = len(s), len(p)
dp = [False] * (n + 1)
dp[0] = True
for j in range(2, n + 1):
if p[j - 1] == '*':
dp[j] = dp[j - 2]
for i in range(1, m + 1):
prev = dp[0]
dp[0] = False
for j in range(1, n + 1):
temp = dp[j]
if p[j - 1] == '*':
dp[j] = dp[j] or dp[j - 2]
if p[j - 2] == '.' or p[j - 2] == s[i - 1]:
dp[j] = dp[j] or prev
else:
dp[j] = prev and (p[j - 1] == '.' or p[j - 1] == s[i - 1])
prev = temp
return dp[n]
print(isMatch('aab', 'c*a*b'))
easy
Solution
Step 1: Trace dp initialization for pattern 'c*a*b'
dp[0] = True; dp[2] = dp[0] because p[1] = '*', dp[4] = dp[2] because p[3] = '*'Step 2: Iterate over string 'aab' updating dp
After processing all characters, dp[n] = dp[6] becomes True because 'c*' matches empty, 'a*' matches 'aa', and 'b' matches 'b'.Final Answer:
Option D -> Option DQuick Check:
Known test case returns True [OK]
Hint: Pattern with '*' can match empty or multiple chars [OK]
Common Mistakes:
- Confusing dp indices or forgetting dp initialization for '*'
2. What is the time complexity of the space-optimized bottom-up DP solution for the longest common substring problem, where n and m are the lengths of the two input strings?
medium
Solution
Step 1: Identify nested loops over n and m
The algorithm uses two nested loops: outer over n and inner over m, leading to O(n*m) iterations.Step 2: Confirm no recursion or substring extraction
Space optimization uses iterative DP with O(m) space, no recursion stack or substring extraction, so no extra multiplicative factors.Final Answer:
Option A -> Option AQuick Check:
Nested loops over both strings dominate time complexity [OK]
Hint: Nested loops over both strings imply O(n*m) time [OK]
Common Mistakes:
- Confusing with linear O(n+m)
- Assuming recursion adds stack space
- Thinking substring extraction adds extra factor
3. What is the time complexity of the optimal palindrome partitioning min cuts algorithm that uses a 1D dp array with palindrome center expansion?
medium
Solution
Step 1: Identify palindrome center expansions
For each of the n centers, expansions can take up to O(n) time in worst case.Step 2: Calculate total time complexity
Since there are n centers and each can expand up to n, total time is O(n^2). No substring slicing or extra nested loops increase complexity beyond quadratic.Final Answer:
Option B -> Option BQuick Check:
Quadratic time matches palindrome center expansion approach [OK]
Hint: Palindrome expansions around centers lead to O(n^2) time [OK]
Common Mistakes:
- Assuming linear time due to single loop
- Confusing with cubic time from naive substring checks
- Thinking binary search applies here
4. Suppose the problem is modified so that characters can be deleted multiple times (i.e., you can delete the same character repeatedly if it appears multiple times), and you want to find the minimum number of deletions to make the two strings equal. Which modification to the DP approach is necessary to handle this variant correctly?
hard
Solution
Step 1: Understand repeated deletions mean characters can be deleted multiple times
This breaks the assumption that each character is deleted at most once.Step 2: Modify DP recurrence to consider repeated deletions
DP must allow transitions that delete characters multiple times, possibly by adjusting state or recurrence to reflect reuse.Step 3: Recognize original DP does not handle repeated deletions explicitly
Original DP assumes each character is deleted once; greedy or no change fail to capture repeated deletions correctly.Final Answer:
Option D -> Option DQuick Check:
Repeated deletions require recurrence changes, not just dimensions or greedy [OK]
Hint: Repeated deletions -> recurrence must allow reuse [OK]
Common Mistakes:
- Assuming original DP suffices
- Adding unnecessary DP dimensions
- Using greedy incorrectly
5. Suppose you want to compute the edit distance where characters can be replaced multiple times (reuse allowed) and the cost of replacement depends on character pairs. Which modification to the classic DP approach correctly handles this variant?
hard
Solution
Step 1: Understand replacement cost depends on character pairs
Replacement cost varies, so DP must incorporate a cost matrix for each character pair.Step 2: Adapt bottom-up DP to use cost matrix in dp updates
When characters differ, dp[j] updates using the specific replacement cost from the matrix, ensuring correct minimal cost.Step 3: Confirm approach handles reuse and varying costs
Bottom-up DP with cost matrix correctly accumulates minimal costs allowing multiple replacements.Final Answer:
Option A -> Option AQuick Check:
DP with cost matrix handles variable replacement costs [OK]
Hint: Incorporate cost matrix into DP transitions for variable replacements [OK]
Common Mistakes:
- Adding extra DP dimension unnecessarily
- Using memoization without cost matrix
- Trying greedy which fails globally
