mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-03 05:27:36 +00:00
docs: rename category folders to remove parentheses and update heap meta title
This commit is contained in:
1 parent
c2a8a31ede
commit
edd37ed0e6
84 files changed
+3
-3
No files matched your search
@@ -0,0 +1,112 @@
|
||||
---
|
||||
title: '208. Implement Trie (Prefix Tree)'
|
||||
description: A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker
|
||||
sidebar:
|
||||
label: 'Implement Trie (Prefix Tree)'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
<Badge variant="accent">Hash Table</Badge>
|
||||
|
||||
### Example 1:
|
||||
- Input: ``
|
||||
- Output: ``
|
||||
- Explanation: Input ["Trie", "insert", "search", "search", "startsWith", "insert", "search"] `[[]`, `["apple"]`, `["apple"]`, `["app"]`, `["app"]`, `["app"]`, ["app"]] Output [null, null, true, false, true, null, true] Explanation Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // return True trie.search("app"); // return False trie.startsWith("app"); // return True trie.insert("app"); trie.search("app"); // return True
|
||||
|
||||
### Constraints:
|
||||
|
||||
- `1 <= word.length, prefix.length <= 2000`
|
||||
- `word` and `prefix` consist only of lowercase English letters.
|
||||
- At most 3 * 10^4 calls in total will be made to insert, search, and startsWith.
|
||||
|
||||
## Approach
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
subgraph state["state — root TrieNode; every node holds a children dict and an endOfWord flag"]
|
||||
R["root"] --- C["... children[c] ..."] --- L["endOfWord = True — a word ends here"]
|
||||
end
|
||||
subgraph insert["insert(word)"]
|
||||
I1["cur = root"] --> I2{"more c in word?"}
|
||||
I2 -- yes --> I3{"c not in cur.children?"}
|
||||
I3 -- yes --> I4["cur.children[c] = TrieNode()"]
|
||||
I4 --> I5["cur = cur.children[c]"]
|
||||
I3 -- no --> I5
|
||||
I5 --> I2
|
||||
I2 -- no --> I6["cur.endOfWord = True"]
|
||||
I6 --> I7(["done"])
|
||||
end
|
||||
subgraph search["search(word)"]
|
||||
S1["cur = root"] --> S2{"more c in word?"}
|
||||
S2 -- yes --> S3{"c not in cur.children?"}
|
||||
S3 -- yes --> S4(["return False"])
|
||||
S3 -- no --> S5["cur = cur.children[c]"]
|
||||
S5 --> S2
|
||||
S2 -- no --> S6(["return cur.endOfWord — the path exists, but is it a whole word?"])
|
||||
end
|
||||
subgraph startsWith["startsWith(prefix)"]
|
||||
P1["cur = root"] --> P2{"more c in prefix?"}
|
||||
P2 -- yes --> P3{"c not in cur.children?"}
|
||||
P3 -- yes --> P4(["return False"])
|
||||
P3 -- no --> P5["cur = cur.children[c]"]
|
||||
P5 --> P2
|
||||
P2 -- no --> P6(["return True — every prefix char had a path"])
|
||||
end
|
||||
state --> insert
|
||||
state --> search
|
||||
state --> startsWith
|
||||
```
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
class TrieNode:
|
||||
def __init__(self):
|
||||
self.children = {}
|
||||
self.endOfWord = False
|
||||
|
||||
|
||||
class Trie:
|
||||
def __init__(self):
|
||||
self.root = TrieNode()
|
||||
|
||||
def insert(self, word: str) -> None:
|
||||
cur = self.root
|
||||
|
||||
for c in word:
|
||||
if c not in cur.children:
|
||||
cur.children[c] = TrieNode()
|
||||
cur = cur.children[c]
|
||||
cur.endOfWord = True
|
||||
|
||||
def search(self, word: str) -> bool:
|
||||
cur = self.root
|
||||
|
||||
for c in word:
|
||||
if c not in cur.children:
|
||||
return False
|
||||
cur = cur.children[c]
|
||||
|
||||
return cur.endOfWord
|
||||
|
||||
def startsWith(self, prefix: str) -> bool:
|
||||
cur = self.root
|
||||
|
||||
for c in prefix:
|
||||
if c not in cur.children:
|
||||
return False
|
||||
cur = cur.children[c]
|
||||
|
||||
return True
|
||||
|
||||
|
||||
# Your Trie object will be instantiated and called as such:
|
||||
# obj = Trie()
|
||||
# obj.insert(word)
|
||||
# param_2 = obj.search(word)
|
||||
# param_3 = obj.startsWith(prefix)
|
||||
```
|
||||
|
||||
## Explanation
|
||||
|
||||
<YouTube id="oobqoCJlHA0" title="Implement Trie (Prefix Tree) - Leetcode 208" />
|
||||
@@ -0,0 +1,106 @@
|
||||
---
|
||||
title: '211. Design Add and Search Words Data Structure'
|
||||
description: Design a data structure that supports adding new words and finding if a string matches any previously added string
|
||||
sidebar:
|
||||
label: 'Design Add and Search Words Data Structure'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
<Badge variant="accent">String</Badge>
|
||||
|
||||
### Constraints:
|
||||
|
||||
- `1 <= word.length <= 25`
|
||||
- `word` in addWord consists of lowercase English letters.
|
||||
- `word` in search consist of '.' or lowercase English letters.
|
||||
- There will be at most 2 dots in `word` for search queries.
|
||||
- At most 10^4 calls will be made to addWord and search.
|
||||
|
||||
## Approach
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
subgraph state["state — root TrieNode; every node holds a children dict and an endOfWord flag"]
|
||||
R["root"] --- C["... children[c] ..."] --- L["endOfWord = True — a word ends here"]
|
||||
end
|
||||
subgraph addWord["addWord(word)"]
|
||||
A1["cur = root"] --> A2{"more c in word?"}
|
||||
A2 -- yes --> A3{"c not in cur.children?"}
|
||||
A3 -- yes --> A4["cur.children[c] = TrieNode()"]
|
||||
A4 --> A5["cur = cur.children[c]"]
|
||||
A3 -- no --> A5
|
||||
A5 --> A2
|
||||
A2 -- no --> A6["cur.endOfWord = True"]
|
||||
A6 --> A7(["done"])
|
||||
end
|
||||
subgraph search["search(word)"]
|
||||
S1["dfs(0, root) — match from index 0 at the root"] --> S2["cur = root"]
|
||||
S2 --> S3{"more i in range(j, len(word))?"}
|
||||
S3 -- no --> S4(["return cur.endOfWord"])
|
||||
S3 -- yes --> S5["c = word[i]"]
|
||||
S5 --> S6{"c == '.'?"}
|
||||
S6 -- yes --> S7{"more child in cur.children.values()?"}
|
||||
S7 -- yes --> S8{"dfs(i + 1, child)? — try the rest of the word from this child"}
|
||||
S8 -- yes --> S9(["return True"])
|
||||
S8 -- no --> S7
|
||||
S7 -- no --> S10(["return False — no child matched the wildcard"])
|
||||
S6 -- no --> S11{"c not in cur.children?"}
|
||||
S11 -- yes --> S12(["return False"])
|
||||
S11 -- no --> S13["cur = cur.children[c]"]
|
||||
S13 --> S3
|
||||
end
|
||||
state --> addWord
|
||||
state --> search
|
||||
```
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
class TrieNode:
|
||||
def __init__(self):
|
||||
self.children = {}
|
||||
self.endOfWord = False
|
||||
|
||||
|
||||
class WordDictionary:
|
||||
def __init__(self):
|
||||
self.root = TrieNode()
|
||||
|
||||
def addWord(self, word: str) -> None:
|
||||
cur = self.root
|
||||
for c in word:
|
||||
if c not in cur.children:
|
||||
cur.children[c] = TrieNode()
|
||||
cur = cur.children[c]
|
||||
cur.endOfWord = True
|
||||
|
||||
# Leverage Recursion
|
||||
def search(self, word: str) -> bool:
|
||||
def dfs(j, root):
|
||||
cur = root
|
||||
for i in range(j, len(word)):
|
||||
c = word[i]
|
||||
|
||||
if c == ".":
|
||||
for child in cur.children.values():
|
||||
if dfs(i + 1, child):
|
||||
return True
|
||||
return False
|
||||
else:
|
||||
if c not in cur.children:
|
||||
return False
|
||||
cur = cur.children[c]
|
||||
return cur.endOfWord
|
||||
|
||||
return dfs(0, self.root)
|
||||
|
||||
|
||||
# Your WordDictionary object will be instantiated and called as such:
|
||||
# obj = WordDictionary()
|
||||
# obj.addWord(word)
|
||||
# param_3 = obj.search(word)
|
||||
```
|
||||
|
||||
## Explanation
|
||||
|
||||
<YouTube id="BTf05gs_8iU" title="Design Add and Search Words Data Structure - Leetcode 211 - Python" />
|
||||
@@ -0,0 +1,3 @@
|
||||
import { defineMeta } from "blume";
|
||||
|
||||
export default defineMeta({ title: "Trie", order: 15 });
|
||||
Reference in new issue
Block a user