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
@@ -16,6 +16,43 @@ sidebar:
|
||||
- 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
|
||||
@@ -63,3 +100,7 @@ class WordDictionary:
|
||||
# obj.addWord(word)
|
||||
# param_2 = obj.search(word)
|
||||
```
|
||||
|
||||
## Explanation
|
||||
|
||||
<YouTube id="BTf05gs_8iU" title="Design Add and Search Words Data Structure - Leetcode 211 - Python" />
|
||||
Reference in new issue
Block a user