mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 21:22:43 +00:00
docs(docs): add Approach diagrams and Explanation sections to LeetCode problem documentation
This commit is contained in:
1 parent
b8c779903f
commit
23eb71d387
19 files changed
+439
No files matched your search
@@ -19,6 +19,44 @@ sidebar:
|
||||
- `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
|
||||
@@ -68,3 +106,7 @@ class Trie:
|
||||
# param_2 = obj.search(word)
|
||||
# param_3 = obj.startsWith(prefix)
|
||||
```
|
||||
|
||||
## Explanation
|
||||
|
||||
<YouTube id="oobqoCJlHA0" title="Implement Trie (Prefix Tree) - Leetcode 208" />
|
||||
Reference in new issue
Block a user