Skip to content

Commit 495c6df

Browse files
authored
feat: update lc problems (doocs#3403)
1 parent 2062cfe commit 495c6df

File tree

25 files changed

+724
-420
lines changed

25 files changed

+724
-420
lines changed

solution/0000-0099/0071.Simplify Path/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ tags:
4545
<p>The trailing slash should be removed.</p>
4646
</div>
4747

48-
<div class="example-block">&nbsp;</div>
49-
5048
<p><strong class="example">Example 2:</strong></p>
5149

5250
<div class="example-block">
@@ -69,6 +67,7 @@ tags:
6967
<p><strong>Explanation:</strong></p>
7068

7169
<p>A double period <code>&quot;..&quot;</code> refers to the directory up a level.</p>
70+
</div>
7271

7372
<p><strong class="example">Example 4:</strong></p>
7473

@@ -81,7 +80,6 @@ tags:
8180

8281
<p>Going one level up from the root directory is not possible.</p>
8382
</div>
84-
</div>
8583

8684
<p><strong class="example">Example 5:</strong></p>
8785

solution/0600-0699/0676.Implement Magic Dictionary/README.md

+12-71
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,32 @@ magicDictionary.search("leetcoded"); // 返回 False
8989

9090
```python
9191
class Trie:
92-
__slots__ = ["children", "is_end"]
92+
__slots__ = "children", "is_end"
9393

9494
def __init__(self):
95-
self.children = {}
95+
self.children: List[Optional[Trie]] = [None] * 26
9696
self.is_end = False
9797

9898
def insert(self, w: str) -> None:
9999
node = self
100100
for c in w:
101-
if c not in node.children:
102-
node.children[c] = Trie()
103-
node = node.children[c]
101+
idx = ord(c) - ord("a")
102+
if node.children[idx] is None:
103+
node.children[idx] = Trie()
104+
node = node.children[idx]
104105
node.is_end = True
105106

106107
def search(self, w: str) -> bool:
107-
def dfs(i: int, node: Trie, diff: int) -> bool:
108+
def dfs(i: int, node: Optional[Trie], diff: int) -> bool:
108109
if i == len(w):
109110
return diff == 1 and node.is_end
110-
if w[i] in node.children and dfs(i + 1, node.children[w[i]], diff):
111+
j = ord(w[i]) - ord("a")
112+
if node.children[j] and dfs(i + 1, node.children[j], diff):
111113
return True
112114
return diff == 0 and any(
113-
dfs(i + 1, node.children[c], 1) for c in node.children if c != w[i]
115+
node.children[k] and dfs(i + 1, node.children[k], 1)
116+
for k in range(26)
117+
if k != j
114118
)
115119

116120
return dfs(0, self, 0)
@@ -506,67 +510,4 @@ impl MagicDictionary {
506510

507511
<!-- solution:end -->
508512

509-
<!-- solution:start -->
510-
511-
### 方法二
512-
513-
<!-- tabs:start -->
514-
515-
#### Python3
516-
517-
```python
518-
class Trie:
519-
__slots__ = ["children", "is_end"]
520-
521-
def __init__(self):
522-
self.children: [Trie | None] = [None] * 26
523-
self.is_end = False
524-
525-
def insert(self, w: str) -> None:
526-
node = self
527-
for c in w:
528-
idx = ord(c) - ord("a")
529-
if node.children[idx] is None:
530-
node.children[idx] = Trie()
531-
node = node.children[idx]
532-
node.is_end = True
533-
534-
def search(self, w: str) -> bool:
535-
def dfs(i: int, node: [Trie | None], diff: int) -> bool:
536-
if i == len(w):
537-
return diff == 1 and node.is_end
538-
j = ord(w[i]) - ord("a")
539-
if node.children[j] and dfs(i + 1, node.children[j], diff):
540-
return True
541-
return diff == 0 and any(
542-
node.children[k] and dfs(i + 1, node.children[k], 1)
543-
for k in range(26)
544-
if k != j
545-
)
546-
547-
return dfs(0, self, 0)
548-
549-
550-
class MagicDictionary:
551-
def __init__(self):
552-
self.trie = Trie()
553-
554-
def buildDict(self, dictionary: List[str]) -> None:
555-
for w in dictionary:
556-
self.trie.insert(w)
557-
558-
def search(self, searchWord: str) -> bool:
559-
return self.trie.search(searchWord)
560-
561-
562-
# Your MagicDictionary object will be instantiated and called as such:
563-
# obj = MagicDictionary()
564-
# obj.buildDict(dictionary)
565-
# param_2 = obj.search(searchWord)
566-
```
567-
568-
<!-- tabs:end -->
569-
570-
<!-- solution:end -->
571-
572513
<!-- problem:end -->

solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md

+12-71
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,32 @@ The time complexity is $O(n \times l + q \times l \times |\Sigma|)$, and the spa
8181

8282
```python
8383
class Trie:
84-
__slots__ = ["children", "is_end"]
84+
__slots__ = "children", "is_end"
8585

8686
def __init__(self):
87-
self.children = {}
87+
self.children: List[Optional[Trie]] = [None] * 26
8888
self.is_end = False
8989

9090
def insert(self, w: str) -> None:
9191
node = self
9292
for c in w:
93-
if c not in node.children:
94-
node.children[c] = Trie()
95-
node = node.children[c]
93+
idx = ord(c) - ord("a")
94+
if node.children[idx] is None:
95+
node.children[idx] = Trie()
96+
node = node.children[idx]
9697
node.is_end = True
9798

9899
def search(self, w: str) -> bool:
99-
def dfs(i: int, node: Trie, diff: int) -> bool:
100+
def dfs(i: int, node: Optional[Trie], diff: int) -> bool:
100101
if i == len(w):
101102
return diff == 1 and node.is_end
102-
if w[i] in node.children and dfs(i + 1, node.children[w[i]], diff):
103+
j = ord(w[i]) - ord("a")
104+
if node.children[j] and dfs(i + 1, node.children[j], diff):
103105
return True
104106
return diff == 0 and any(
105-
dfs(i + 1, node.children[c], 1) for c in node.children if c != w[i]
107+
node.children[k] and dfs(i + 1, node.children[k], 1)
108+
for k in range(26)
109+
if k != j
106110
)
107111

108112
return dfs(0, self, 0)
@@ -498,67 +502,4 @@ impl MagicDictionary {
498502

499503
<!-- solution:end -->
500504

501-
<!-- solution:start -->
502-
503-
### Solution 2
504-
505-
<!-- tabs:start -->
506-
507-
#### Python3
508-
509-
```python
510-
class Trie:
511-
__slots__ = ["children", "is_end"]
512-
513-
def __init__(self):
514-
self.children: [Trie | None] = [None] * 26
515-
self.is_end = False
516-
517-
def insert(self, w: str) -> None:
518-
node = self
519-
for c in w:
520-
idx = ord(c) - ord("a")
521-
if node.children[idx] is None:
522-
node.children[idx] = Trie()
523-
node = node.children[idx]
524-
node.is_end = True
525-
526-
def search(self, w: str) -> bool:
527-
def dfs(i: int, node: [Trie | None], diff: int) -> bool:
528-
if i == len(w):
529-
return diff == 1 and node.is_end
530-
j = ord(w[i]) - ord("a")
531-
if node.children[j] and dfs(i + 1, node.children[j], diff):
532-
return True
533-
return diff == 0 and any(
534-
node.children[k] and dfs(i + 1, node.children[k], 1)
535-
for k in range(26)
536-
if k != j
537-
)
538-
539-
return dfs(0, self, 0)
540-
541-
542-
class MagicDictionary:
543-
def __init__(self):
544-
self.trie = Trie()
545-
546-
def buildDict(self, dictionary: List[str]) -> None:
547-
for w in dictionary:
548-
self.trie.insert(w)
549-
550-
def search(self, searchWord: str) -> bool:
551-
return self.trie.search(searchWord)
552-
553-
554-
# Your MagicDictionary object will be instantiated and called as such:
555-
# obj = MagicDictionary()
556-
# obj.buildDict(dictionary)
557-
# param_2 = obj.search(searchWord)
558-
```
559-
560-
<!-- tabs:end -->
561-
562-
<!-- solution:end -->
563-
564505
<!-- problem:end -->

solution/0600-0699/0676.Implement Magic Dictionary/Solution2.py

-48
This file was deleted.

0 commit comments

Comments
 (0)