- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 43 lines of Python from the credited upstream file 3093.py.
- The implementation visibly relies on sequence storage, hash lookup.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class TrieNode:2 def __init__(self):3 self.children: dict[str, TrieNode] = {}4 self.isWord = False5 self.length = math.inf6 self.index = -17 8 9class Solution:10 def stringIndices(11 self,12 wordsContainer: list[str],13 wordsQuery: list[str],14 ) -> list[int]:15 ans = []16 root = TrieNode()17 minIndex = min(enumerate(wordsContainer), key=lambda x: len(x[1]))[0]18 19 def insert(word: str, index: int) -> None:20 node = root21 for c in reversed(word):22 node = node.children.setdefault(c, TrieNode())23 if node.length > len(word):24 node.length = len(word)25 node.index = index26 27 def search(word: str) -> int:28 node = root29 for c in reversed(word):30 if c not in node.children:31 return node.index32 node = node.children[c]33 return node.index34 35 for i, word in enumerate(wordsContainer):36 insert(word, i)37 38 for query in wordsQuery:39 index = search(query)40 ans.append(minIndex if index == -1 else index)41 42 return ans43