Skip to content

Commit 62de943

Browse files
committed
✨feat: Add 819
1 parent 9a331c0 commit 62de943

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
| [796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-string/solution/by-ac_oier-bnkx/) | 简单 | 🤩🤩🤩 |
9090
| [804. 唯一摩尔斯密码词](https://leetcode-cn.com/problems/unique-morse-code-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/unique-morse-code-words/solution/by-ac_oier-a9hv/) | 简单 | 🤩🤩🤩 |
9191
| [806. 写字符串需要的行数](https://leetcode-cn.com/problems/number-of-lines-to-write-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-lines-to-write-string/solution/by-ac_oier-5hg2/) | 简单 | 🤩🤩🤩🤩 |
92+
| [819. 最常见的单词](https://leetcode-cn.com/problems/most-common-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/most-common-word/solution/by-ac_oier-6aqd/) | 简单 | 🤩🤩🤩🤩 |
9293
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
9394
| [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/buddy-strings/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-q056/) | 简单 | 🤩🤩🤩🤩🤩 |
9495
| [867. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/transpose-matrix/solution/yi-you-wei-jin-huo-xu-ni-neng-kan-kan-zh-m53m/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[819. 最常见的单词](https://leetcode-cn.com/problems/most-common-word/solution/by-ac_oier-6aqd/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「哈希表」
6+
7+
8+
9+
给定一个段落 (`paragraph`) 和一个禁用单词列表 (`banned`)。返回出现次数最多,同时不在禁用列表中的单词。
10+
11+
题目保证至少有一个词不在禁用列表中,而且答案唯一。
12+
13+
禁用列表中的单词用小写字母表示,不含标点符号。段落中的单词不区分大小写。答案都是小写字母。
14+
15+
示例:
16+
```
17+
输入:
18+
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
19+
banned = ["hit"]
20+
21+
输出: "ball"
22+
23+
解释:
24+
"hit" 出现了3次,但它是一个禁用的单词。
25+
"ball" 出现了2次 (同时没有其他单词出现2次),所以它是段落里出现次数最多的,且不在禁用列表中的单词。
26+
注意,所有这些单词在段落里不区分大小写,标点符号需要忽略(即使是紧挨着单词也忽略, 比如 "ball,"),
27+
"hit"不是最终的答案,虽然它出现次数更多,但它在禁用单词列表中。
28+
```
29+
30+
提示:
31+
* $1 <= `段落长度` <= 1000$
32+
* $0 <= `禁用单词个数` <= 100$
33+
* $1 <= `禁用单词长度` <= 10$
34+
* 答案是唯一的, 且都是小写字母 (即使在 `paragraph` 里是大写的,即使是一些特定的名词,答案都是小写的。)
35+
* `paragraph` 只包含字母、空格和下列标点符号`!?',;.`
36+
* 不存在没有连字符或者带有连字符的单词。
37+
* 单词里只包含字母,不会出现省略号或者其他标点符号。
38+
39+
---
40+
41+
### 模拟
42+
43+
根据题意进行模拟即可。
44+
45+
代码:
46+
```Java
47+
class Solution {
48+
public String mostCommonWord(String s, String[] banned) {
49+
Set<String> set = new HashSet<>();
50+
for (String b : banned) set.add(b);
51+
char[] cs = s.toCharArray();
52+
int n = cs.length;
53+
String ans = null;
54+
Map<String, Integer> map = new HashMap<>();
55+
for (int i = 0; i < n; ) {
56+
if (!Character.isLetter(cs[i]) && ++i >= 0) continue;
57+
int j = i;
58+
while (j < n && Character.isLetter(cs[j])) j++;
59+
String sub = s.substring(i, j).toLowerCase();
60+
i = j + 1;
61+
if (set.contains(sub)) continue;
62+
map.put(sub, map.getOrDefault(sub, 0) + 1);
63+
if (ans == null || map.get(sub) > map.get(ans)) ans = sub;
64+
}
65+
return ans;
66+
}
67+
}
68+
```
69+
* 时间复杂度:$O(n + m)$,$n$ 和 $m$ 分别代表 `s` 的字符总长度和 `banned` 的字符总长度(哈希函数的计算与长度成正比)
70+
* 空间复杂度:$O(n + m)$
71+
72+
---
73+
74+
### 最后
75+
76+
这是我们「刷穿 LeetCode」系列文章的第 `No.819` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
77+
78+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
79+
80+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
81+
82+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
83+

0 commit comments

Comments
 (0)