Skip to content

feat: update lc problems #3403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions solution/0000-0099/0071.Simplify Path/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ tags:
<p>The trailing slash should be removed.</p>
</div>

<div class="example-block">&nbsp;</div>

<p><strong class="example">Example 2:</strong></p>

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

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

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

Expand All @@ -81,7 +80,6 @@ tags:

<p>Going one level up from the root directory is not possible.</p>
</div>
</div>

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

Expand Down
83 changes: 12 additions & 71 deletions solution/0600-0699/0676.Implement Magic Dictionary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,32 @@ magicDictionary.search("leetcoded"); // 返回 False

```python
class Trie:
__slots__ = ["children", "is_end"]
__slots__ = "children", "is_end"

def __init__(self):
self.children = {}
self.children: List[Optional[Trie]] = [None] * 26
self.is_end = False

def insert(self, w: str) -> None:
node = self
for c in w:
if c not in node.children:
node.children[c] = Trie()
node = node.children[c]
idx = ord(c) - ord("a")
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.is_end = True

def search(self, w: str) -> bool:
def dfs(i: int, node: Trie, diff: int) -> bool:
def dfs(i: int, node: Optional[Trie], diff: int) -> bool:
if i == len(w):
return diff == 1 and node.is_end
if w[i] in node.children and dfs(i + 1, node.children[w[i]], diff):
j = ord(w[i]) - ord("a")
if node.children[j] and dfs(i + 1, node.children[j], diff):
return True
return diff == 0 and any(
dfs(i + 1, node.children[c], 1) for c in node.children if c != w[i]
node.children[k] and dfs(i + 1, node.children[k], 1)
for k in range(26)
if k != j
)

return dfs(0, self, 0)
Expand Down Expand Up @@ -506,67 +510,4 @@ impl MagicDictionary {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Trie:
__slots__ = ["children", "is_end"]

def __init__(self):
self.children: [Trie | None] = [None] * 26
self.is_end = False

def insert(self, w: str) -> None:
node = self
for c in w:
idx = ord(c) - ord("a")
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.is_end = True

def search(self, w: str) -> bool:
def dfs(i: int, node: [Trie | None], diff: int) -> bool:
if i == len(w):
return diff == 1 and node.is_end
j = ord(w[i]) - ord("a")
if node.children[j] and dfs(i + 1, node.children[j], diff):
return True
return diff == 0 and any(
node.children[k] and dfs(i + 1, node.children[k], 1)
for k in range(26)
if k != j
)

return dfs(0, self, 0)


class MagicDictionary:
def __init__(self):
self.trie = Trie()

def buildDict(self, dictionary: List[str]) -> None:
for w in dictionary:
self.trie.insert(w)

def search(self, searchWord: str) -> bool:
return self.trie.search(searchWord)


# Your MagicDictionary object will be instantiated and called as such:
# obj = MagicDictionary()
# obj.buildDict(dictionary)
# param_2 = obj.search(searchWord)
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
83 changes: 12 additions & 71 deletions solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,32 @@ The time complexity is $O(n \times l + q \times l \times |\Sigma|)$, and the spa

```python
class Trie:
__slots__ = ["children", "is_end"]
__slots__ = "children", "is_end"

def __init__(self):
self.children = {}
self.children: List[Optional[Trie]] = [None] * 26
self.is_end = False

def insert(self, w: str) -> None:
node = self
for c in w:
if c not in node.children:
node.children[c] = Trie()
node = node.children[c]
idx = ord(c) - ord("a")
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.is_end = True

def search(self, w: str) -> bool:
def dfs(i: int, node: Trie, diff: int) -> bool:
def dfs(i: int, node: Optional[Trie], diff: int) -> bool:
if i == len(w):
return diff == 1 and node.is_end
if w[i] in node.children and dfs(i + 1, node.children[w[i]], diff):
j = ord(w[i]) - ord("a")
if node.children[j] and dfs(i + 1, node.children[j], diff):
return True
return diff == 0 and any(
dfs(i + 1, node.children[c], 1) for c in node.children if c != w[i]
node.children[k] and dfs(i + 1, node.children[k], 1)
for k in range(26)
if k != j
)

return dfs(0, self, 0)
Expand Down Expand Up @@ -498,67 +502,4 @@ impl MagicDictionary {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Trie:
__slots__ = ["children", "is_end"]

def __init__(self):
self.children: [Trie | None] = [None] * 26
self.is_end = False

def insert(self, w: str) -> None:
node = self
for c in w:
idx = ord(c) - ord("a")
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.is_end = True

def search(self, w: str) -> bool:
def dfs(i: int, node: [Trie | None], diff: int) -> bool:
if i == len(w):
return diff == 1 and node.is_end
j = ord(w[i]) - ord("a")
if node.children[j] and dfs(i + 1, node.children[j], diff):
return True
return diff == 0 and any(
node.children[k] and dfs(i + 1, node.children[k], 1)
for k in range(26)
if k != j
)

return dfs(0, self, 0)


class MagicDictionary:
def __init__(self):
self.trie = Trie()

def buildDict(self, dictionary: List[str]) -> None:
for w in dictionary:
self.trie.insert(w)

def search(self, searchWord: str) -> bool:
return self.trie.search(searchWord)


# Your MagicDictionary object will be instantiated and called as such:
# obj = MagicDictionary()
# obj.buildDict(dictionary)
# param_2 = obj.search(searchWord)
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
48 changes: 0 additions & 48 deletions solution/0600-0699/0676.Implement Magic Dictionary/Solution2.py

This file was deleted.

Loading
Loading