Practice
Solution
Step 1: Understand the problem constraints
The problem requires replacing words with the shortest root prefix efficiently, which suggests a prefix-based data structure.Step 2: Evaluate approaches
Brute force and sorting with binary search are less efficient due to repeated prefix checks. Dynamic programming is not suitable as the problem is not about overlapping subproblems but prefix matching. A trie efficiently stores prefixes and allows quick shortest prefix search.Final Answer:
Option D -> Option DQuick Check:
Trie enables O(m*k) prefix search, optimal for this problem [OK]
- Thinking DP applies here
- Using brute force without pruning
shortest_unique_prefix(words)?Solution
Step 1: Trace insertion frequencies
Insert "dog": d(1), o(1), g(1); "dove": d(2), o(2), v(1), e(1); "duck": d(3), u(1), c(1), k(1).Step 2: Find unique prefixes
For "dog": prefix "dog" (g freq=1) unique; for "dove": prefix "dov" (v freq=1) unique; for "duck": prefix "du" (u freq=1) unique.Final Answer:
Option D -> Option DQuick Check:
Prefixes match first node with freq=1 [OK]
- Stopping prefix too early
- Assuming first letter always unique
class TrieNode:
def __init__(self):
self.children = {}
self.is_word = False
class StreamChecker:
def __init__(self, words):
self.root = TrieNode()
self.max_len = max(len(w) for w in words)
self.stream = []
for word in words:
node = self.root
for ch in reversed(word):
if ch not in node.children:
node.children[ch] = TrieNode()
node = node.children[ch]
node.is_word = True
def query(self, letter):
self.stream.append(letter)
if len(self.stream) > self.max_len:
self.stream.pop(0)
node = self.root
for ch in reversed(self.stream):
if ch not in node.children:
return False
node = node.children[ch]
if node.is_word:
return True
return False
sc = StreamChecker(["cd", "f", "kl"])
print(sc.query('a'))
print(sc.query('b'))
print(sc.query('c'))
print(sc.query('d'))
Solution
Step 1: Trace the stream buffer after queries
After queries 'a', 'b', 'c', 'd', the stream buffer contains ['a','b','c','d'], but max_len=2, so oldest letters are popped. After 'c', stream=['b','c'], after 'd', stream=['c','d'].Step 2: Traverse trie with reversed stream
For query('d'), reversed stream is ['d','c']. Trie has reversed words: 'dc' for 'cd'. Traversing 'd' then 'c' hits is_word=True at 'c' node, so returns True. However, the query sequence is query('a'), query('b'), query('c'), query('d'). The query('c') returns False because 'c' alone is not a word. The query('d') returns True because 'cd' is in the dictionary and the stream buffer contains ['c','d'].Step 3: Check query('d') return value
Since the query('d') returns True, the correct answer is True.Final Answer:
Option B -> Option BQuick Check:
Stream buffer limited to max word length matches 'cd' suffix [OK]
- Not limiting stream buffer size
- Not reversing words in trie
- Returning false too early
s = "leetcode" and wordDict = ["leet", "code"], what is the value of dp[8] after the loop completes?Solution
Step 1: Trace dp array updates for s="leetcode"
dp[0] = True initially. For i=4, substring s[0:4] = "leet" is in dictionary, so dp[4] = True. For i=8, substring s[4:8] = "code" is in dictionary and dp[4] is True, so dp[8] = True.Step 2: Confirm final dp[n] value
Since the entire string can be segmented, dp[8] = True.Final Answer:
Option D -> Option DQuick Check:
dp correctly marks segmentable prefixes [OK]
- Off-by-one errors in dp indexing
- Not breaking early when dp[i] is true
- Confusing reversed trie traversal
Solution
Step 1: Understand the reuse requirement
Allowing repeated letters and concatenations means suffix matching alone is insufficient; need to handle overlapping patterns efficiently.Step 2: Evaluate data structures
DFS on trie is expensive and can degrade query time. Counting repeated letters is complex and error-prone. Increasing buffer size is inefficient. Suffix automaton or Aho-Corasick automaton efficiently handle repeated patterns and multiple matches.Step 3: Choose optimal solution
Using suffix or Aho-Corasick automaton supports repeated pattern matching with efficient query time.Final Answer:
Option D -> Option DQuick Check:
Suffix automaton handles repeated substrings efficiently [OK]
- Trying DFS on trie causing exponential time
- Increasing buffer size unboundedly
- Tracking counts in trie nodes incorrectly
