This commit is contained in:
2026-06-05 22:18:39 +08:00
parent 0aed1528e2
commit 14d05011d5
7 changed files with 169 additions and 7 deletions
@@ -52,10 +52,40 @@ Write your approach here.
** TODO Python
#+begin_src python
from collections import defaultdict as dd
class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
words = reversed(sorted(words))
def squash(word, trie):
lst = []
for c in word:
lst.append([trie, ''])
for i in range(len(lst)):
lst[i][0] = lst[i][0][c]
lst[i][1] += c
if not lst[i][0]['ads']:
lst[i][0]['ads'] = {lst[i][1]: [word]}
else:
lst[i][0]['ads'][lst[i][1]].append(word)
def in_trie(word, trie):
for c in word:
if c not in trie:
return []
trie = trie[c]
return [(word, bw) for bw in trie['ads'][word]]
trie_maker = lambda: dd(trie_maker)
trie = trie_maker()
ans = []
for s in words:
ans += in_trie(s, trie)
squash(s, trie)
return ans
#+end_src
**
** TODO C++
#+begin_src cpp
class Solution {