Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
leetcode: finished #211
Browse files Browse the repository at this point in the history
  • Loading branch information
xqm32 committed Feb 16, 2024
1 parent 7e03eb1 commit c0087bb
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions content/leetcode/2024/2.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,51 @@ title: 2024.2
draft: false
---

# 2024.2.16

```python
#
# @lc app=leetcode.cn id=211 lang=python3
#
# [211] 添加与搜索单词 - 数据结构设计
#

# @lc code=start
class WordDictionary:
def __init__(self):
self.trie = {}
self.end_of_word = "#"

def addWord(self, word: str) -> None:
node = self.trie
for char in word:
node = node.setdefault(char, {})
node[self.end_of_word] = self.end_of_word

def search(self, word: str) -> bool:
def dfs(index, node):
if index == len(word):
return self.end_of_word in node
if word[index] != ".":
if word[index] not in node:
return False
return dfs(index + 1, node[word[index]])
else:
for child in node:
if child != self.end_of_word and dfs(index + 1, node[child]):
return True
return False

return dfs(0, self.trie)


# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
# @lc code=end
```

# 2024.2.15

```python
Expand Down

0 comments on commit c0087bb

Please sign in to comment.