forked from itcharge/AlgoNote
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] main from itcharge:main #132
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
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
039bba1
Create 1551. 使数组中所有元素相等的最小操作数.md
itcharge 31bb59d
Create 1716. 计算力扣银行的钱.md
itcharge 45010d5
Create 0819. 最常见的单词.md
itcharge f3fa97e
更新题解列表
itcharge bb2a351
Update 01.Array-Bubble-Sort.md
itcharge 7cce26c
Update 01.Array-Two-Pointers.md
itcharge 351c2ef
Update 01.Linked-List-Sort.md
itcharge cab31fe
Update LinkedList-BucketSort.py
itcharge e2924cb
Update Array-BubbleSort.py
itcharge 4a2b277
Update 01.Binary-Search-Tree.md
itcharge 01070d9
更新分类题解列表 + 更正错误
itcharge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create 0819. 最常见的单词.md
- Loading branch information
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
Solutions/0819. 最常见的单词.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# [0819. 最常见的单词](https://leetcode.cn/problems/most-common-word/) | ||
|
||
- 标签:哈希表、字符串、计数 | ||
- 难度:简单 | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定一个字符串 $paragraph$ 表示段落,再给定搞一个禁用单词列表 $banned$。 | ||
|
||
**要求**:返回出现次数最多,同时不在禁用列表中的单词。 | ||
|
||
**说明**: | ||
|
||
- 题目保证至少有一个词不在禁用列表中,而且答案唯一。 | ||
- 禁用列表 $banned$ 中的单词用小写字母表示,不含标点符号。 | ||
- 段落 $paragraph$ 只包含字母、空格和下列标点符号`!?',;.` | ||
- 段落中的单词不区分大小写。 | ||
- 1ドル \le 段落长度 \le 1000$。 | ||
- 0ドル \le 禁用单词个数 \le 100$。 | ||
- 1ドル \le 禁用单词长度 \le 10$。 | ||
- 答案是唯一的,且都是小写字母(即使在 $paragraph$ 里是大写的,即使是一些特定的名词,答案都是小写的)。 | ||
- 不存在没有连字符或者带有连字符的单词。 | ||
- 单词里只包含字母,不会出现省略号或者其他标点符号。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```Python | ||
输入: | ||
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." | ||
banned = ["hit"] | ||
输出: "ball" | ||
解释: | ||
"hit" 出现了3次,但它是一个禁用的单词。 | ||
"ball" 出现了2次 (同时没有其他单词出现2次),所以它是段落里出现次数最多的,且不在禁用列表中的单词。 | ||
注意,所有这些单词在段落里不区分大小写,标点符号需要忽略(即使是紧挨着单词也忽略, 比如 "ball,"), | ||
"hit"不是最终的答案,虽然它出现次数更多,但它在禁用单词列表中。 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```Python | ||
输入: | ||
paragraph = "a." | ||
banned = [] | ||
输出:"a" | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:哈希表 | ||
|
||
1. 将禁用词列表转为集合 $banned\underline{}set$。 | ||
2. 遍历段落 $paragraph,ドル获取段落中的所有单词。 | ||
3. 判断当前单词是否在禁用词集合中,如果不在禁用词集合中,则使用哈希表对该单词进行计数。 | ||
4. 遍历完,找出哈希表中频率最大的单词,将该单词作为答案进行返回。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: | ||
banned_set = set(banned) | ||
cnts = Counter() | ||
|
||
word = "" | ||
for ch in paragraph: | ||
if ch.isalpha(): | ||
word += ch.lower() | ||
else: | ||
if word and word not in banned_set: | ||
cnts[word] += 1 | ||
word = "" | ||
if word and word not in banned_set: | ||
cnts[word] += 1 | ||
|
||
max_cnt, ans = 0, "" | ||
for word, cnt in cnts.items(): | ||
if cnt > max_cnt: | ||
max_cnt = cnt | ||
ans = word | ||
|
||
return ans | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n + m),ドル其中 $n$ 为段落 $paragraph$ 的长度,$m$ 是禁用词 $banned$ 的长度。 | ||
- **空间复杂度**:$O(n + m)$。 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.