Practice
Solution
Step 1: Understand problem constraints
The problem requires exploring all combinations of letter cases, which is a classic subsets problem where each letter can be included as lowercase or uppercase.Step 2: Identify suitable algorithm
Backtracking with include/exclude choices for each letter's case systematically explores all 2^k combinations, ensuring completeness and correctness.Final Answer:
Option A -> Option AQuick Check:
Backtracking explores all subsets of letter cases [OK]
- Thinking greedy can find all permutations
- Using DP that doesn't fit subsets pattern
"a1b"?
from typing import List
def letterCasePermutation(s: str) -> List[str]:
res = []
arr = list(s)
def backtrack(i: int):
if i == len(arr):
res.append(''.join(arr))
return
if arr[i].isdigit():
backtrack(i + 1)
else:
arr[i] = arr[i].lower()
backtrack(i + 1)
arr[i] = arr[i].upper()
backtrack(i + 1)
arr[i] = s[i] # revert to original
backtrack(0)
return res
print(letterCasePermutation("a1b"))Solution
Step 1: Trace backtrack calls for input "a1b"
Index 0 is 'a' (letter), toggled to 'a' then 'A'. Index 1 is '1' (digit), skipped. Index 2 is 'b' (letter), toggled to 'b' then 'B'.Step 2: Collect permutations in order
Order of appending: 'a1b', 'a1B', 'A1b', 'A1B'.Final Answer:
Option C -> Option CQuick Check:
Output matches expected toggling order [OK]
- Swapping order of uppercase/lowercase calls
- Misplacing digit handling
k from 1 to n. Which line contains the subtle bug that causes incorrect or duplicate results?Solution
Step 1: Identify how results are stored
Appending path directly stores a reference, so all entries in result point to the same list object.Step 2: Understand consequence
All combinations in result become identical after backtracking modifies path, causing duplicates or incorrect results.Final Answer:
Option D -> Option DQuick Check:
Appending a copy (path[:]) fixes the bug [OK]
- Forgetting to copy path before appending to result
def largestDivisibleSubset(nums):
if not nums:
return []
n = len(nums)
dp = [1] * n
prev = [-1] * n
max_index = 0
for i in range(n):
for j in range(i - 1, -1, -1):
if nums[j] % nums[i] == 0:
if dp[j] + 1 > dp[i]:
dp[i] = dp[j] + 1
prev[i] = j
else:
break
if dp[i] > dp[max_index]:
max_index = i
result = []
while max_index >= 0:
result.append(nums[max_index])
max_index = prev[max_index]
return result[::-1]
Solution
Step 1: Check divisibility condition
The condition should be nums[i] % nums[j] == 0 because we want to check if current number is divisible by a smaller number.Step 2: Understand impact of bug
Using nums[j] % nums[i] == 0 reverses the divisibility logic, causing incorrect dp updates and wrong subsets.Final Answer:
Option D -> Option DQuick Check:
Correct divisibility check is crucial for correctness [OK]
- Swapping divisor and dividend in modulo check
- Not sorting input before DP
- Incorrect subset reconstruction
Solution
Step 1: Identify main operations affecting complexity
Sorting takes O(n log n), which is dominated by subset generation. The algorithm generates all subsets (2^n) and copies subsets of average length O(n).Step 2: Calculate total time complexity
Generating all subsets is O(2^n), and copying each subset of length up to n leads to O(2^n * n) total time.Final Answer:
Option C -> Option CQuick Check:
Copying subsets causes the extra factor n, not just 2^n [OK]
- Ignoring subset copying cost and saying O(2^n)
- Confusing sorting cost as dominant
- Mistaking nested loops as O(n^2)
