Trie Advanced¶
Table of Contents¶
- 676. Implement Magic Dictionary (Medium)
- 212. Word Search II (Hard)
- 3093. Longest Common Suffix Queries (Hard)
- 745. Prefix and Suffix Search (Hard)
- 3045. Count Prefix and Suffix Pairs II (Hard)
- 336. Palindrome Pairs (Hard)
- 1948. Delete Duplicate Folders in System (Hard)
- 425. Word Squares (Hard) 👑
- 527. Word Abbreviation (Hard) 👑
- 588. Design In-Memory File System (Hard) 👑
- 616. Add Bold Tag in String (Medium) 👑
- 758. Bold Words in String (Medium) 👑
- 642. Design Search Autocomplete System (Hard) 👑
- 1065. Index Pairs of a String (Easy) 👑
- 1166. Design File System (Medium) 👑
- 1858. Longest Word With All Prefixes (Medium) 👑
676. Implement Magic Dictionary¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, depth first search, design, trie
212. Word Search II¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, string, backtracking, trie, matrix
from typing import List
from template import TrieNode
# Backtracking + Trie
def findWords(board: List[List[str]], words: List[str]) -> List[str]:
root = TrieNode()
for word in words:
root.addWord(word)
m, n = len(board), len(board[0])
result, visit = set(), set()
def dfs(r, c, node, word):
if (
r < 0
or r >= m
or c < 0
or c >= n
or (r, c) in visit
or board[r][c] not in node.children
):
return None
visit.add((r, c))
node = node.children[board[r][c]]
word += board[r][c]
if node.isWord:
result.add(word)
dfs(r - 1, c, node, word)
dfs(r + 1, c, node, word)
dfs(r, c - 1, node, word)
dfs(r, c + 1, node, word)
visit.remove((r, c))
for r in range(m):
for c in range(n):
dfs(r, c, root, "")
return list(result)
board = [
["o", "a", "a", "n"],
["e", "t", "a", "e"],
["i", "h", "k", "r"],
["i", "f", "l", "v"],
]
words = ["oath", "pea", "eat", "rain"]
print(findWords(board, words))
# ['eat', 'oath']
3093. Longest Common Suffix Queries¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, string, trie
745. Prefix and Suffix Search¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, hash table, string, design, trie
3045. Count Prefix and Suffix Pairs II¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, string, trie, rolling hash, string matching, hash function
336. Palindrome Pairs¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, hash table, string, trie
1948. Delete Duplicate Folders in System¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, hash table, string, trie, hash function
425. Word Squares¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, string, backtracking, trie
527. Word Abbreviation¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, string, greedy, trie, sorting
588. Design In-Memory File System¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: hash table, string, design, trie, sorting
from collections import defaultdict
class TrieNode:
def __init__(self):
self.children = defaultdict(TrieNode)
self.content = ""
# Trie
class FileSystem:
def __init__(self):
self.root = TrieNode()
def ls(self, path: str) -> list:
cur = self.root
if path != "/":
paths = path.split("/")[1:]
for p in paths:
cur = cur.children[p]
if cur.content:
return [path.split("/")[-1]]
return sorted(cur.children.keys())
def mkdir(self, path: str) -> None:
cur = self.root
paths = path.split("/")[1:]
for p in paths:
cur = cur.children[p]
def addContentToFile(self, filePath: str, content: str) -> None:
cur = self.root
paths = filePath.split("/")[1:]
for p in paths:
cur = cur.children[p]
cur.content += content
def readContentFromFile(self, filePath: str) -> str:
cur = self.root
paths = filePath.split("/")[1:]
for p in paths:
cur = cur.children[p]
return cur.content
obj = FileSystem()
obj.mkdir("/a/b/c")
obj.addContentToFile("/a/b/c/d", "hello")
print(obj.ls("/")) # ["a"]
print(obj.readContentFromFile("/a/b/c/d")) # "hello"
616. Add Bold Tag in String¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, string, trie, string matching
758. Bold Words in String¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, string, trie, string matching
642. Design Search Autocomplete System¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, depth first search, design, trie, sorting, heap priority queue, data stream
1065. Index Pairs of a String¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, string, trie, sorting
1166. Design File System¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, design, trie
from collections import defaultdict
class TrieNode:
def __init__(self, name):
self.name = name
self.children = defaultdict(TrieNode)
self.value = -1
# Trie
class FileSystem:
def __init__(self):
self.root = TrieNode("")
def createPath(self, path: str, value: int) -> bool:
paths = path.split("/")[1:]
cur = self.root
for idx, path in enumerate(paths):
if path not in cur.children:
if idx == len(paths) - 1:
cur.children[path] = TrieNode(path)
else:
return False
cur = cur.children[path]
if cur.value != -1:
return False
cur.value = value
return True
def get(self, path: str) -> int:
cur = self.root
paths = path.split("/")[1:]
for path in paths:
if path not in cur.children:
return -1
cur = cur.children[path]
return cur.value
# Your FileSystem object will be instantiated and called as such:
path = "/a"
value = 1
obj = FileSystem()
print(obj.createPath(path, value)) # False
print(obj.get(path)) # 1
1858. Longest Word With All Prefixes¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: depth first search, trie