Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 977a6bf

Browse files
feat: add weekly contest 416 (doocs#3551)
1 parent accb72e commit 977a6bf

File tree

32 files changed

+2285
-0
lines changed

32 files changed

+2285
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3295.Report%20Spam%20Message/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3295. 举报垃圾信息](https://leetcode.cn/problems/report-spam-message)
10+
11+
[English Version](/solution/3200-3299/3295.Report%20Spam%20Message/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个字符串数组 <code>message</code> 和一个字符串数组 <code>bannedWords</code>。</p>
18+
19+
<p>如果数组中 <strong>至少</strong> 存在两个单词与 <code>bannedWords</code> 中的任一单词 <strong>完全相同</strong>,则该数组被视为 <strong>垃圾信息</strong>。</p>
20+
21+
<p>如果数组 <code>message</code> 是垃圾信息,则返回 <code>true</code>;否则返回 <code>false</code>。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>输入:</strong> <span class="example-io">message = ["hello","world","leetcode"], bannedWords = ["world","hello"]</span></p>
29+
30+
<p><strong>输出:</strong> <span class="example-io">true</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p>数组 <code>message</code> 中的 <code>"hello"</code> 和 <code>"world"</code> 都出现在数组 <code>bannedWords</code> 中。</p>
35+
</div>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>输入:</strong> <span class="example-io">message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</span></p>
41+
42+
<p><strong>输出:</strong> <span class="example-io">false</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<p>数组 <code>message</code> 中只有一个单词(<code>"programming"</code>)出现在数组 <code>bannedWords</code> 中。</p>
47+
</div>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= message.length, bannedWords.length &lt;= 10<sup>5</sup></code></li>
55+
<li><code>1 &lt;= message[i].length, bannedWords[i].length &lt;= 15</code></li>
56+
<li><code>message[i]</code> 和 <code>bannedWords[i]</code> 都只由小写英文字母组成。</li>
57+
</ul>
58+
59+
<!-- description:end -->
60+
61+
## 解法
62+
63+
<!-- solution:start -->
64+
65+
### 方法一:哈希表
66+
67+
我们用一个哈希表 $s$ 存储 $\textit{bannedWords}$ 中的所有单词,然后遍历 $\textit{message}$ 中的每个单词,如果单词在哈希表 $s$ 中出现,我们就将计数器 $cnt$ 加一,如果 $cnt$ 大于等于 2ドル,ドル我们就返回 $\text{true},ドル否则返回 $\text{false}$。
68+
69+
时间复杂度 $O((n + m) \times |w|),ドル空间复杂度 $O(m \times |w|)$。其中 $n$ 是数组 $\textit{message}$ 的长度,而 $m$ 和 $|w|$ 分别是数组 $\textit{bannedWords}$ 的长度和数组中单词的最大长度。
70+
71+
<!-- tabs:start -->
72+
73+
#### Python3
74+
75+
```python
76+
class Solution:
77+
def reportSpam(self, message: List[str], bannedWords: List[str]) -> bool:
78+
s = set(bannedWords)
79+
return sum(w in s for w in message) >= 2
80+
```
81+
82+
#### Java
83+
84+
```java
85+
class Solution {
86+
public boolean reportSpam(String[] message, String[] bannedWords) {
87+
Set<String> s = new HashSet<>();
88+
for (var w : bannedWords) {
89+
s.add(w);
90+
}
91+
int cnt = 0;
92+
for (var w : message) {
93+
if (s.contains(w) && ++cnt >= 2) {
94+
return true;
95+
}
96+
}
97+
return false;
98+
}
99+
}
100+
```
101+
102+
#### C++
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
bool reportSpam(vector<string>& message, vector<string>& bannedWords) {
108+
unordered_set<string> s(bannedWords.begin(), bannedWords.end());
109+
int cnt = 0;
110+
for (const auto& w : message) {
111+
if (s.contains(w) && ++cnt >= 2) {
112+
return true;
113+
}
114+
}
115+
return false;
116+
}
117+
};
118+
```
119+
120+
#### Go
121+
122+
```go
123+
func reportSpam(message []string, bannedWords []string) bool {
124+
s := map[string]bool{}
125+
for _, w := range bannedWords {
126+
s[w] = true
127+
}
128+
cnt := 0
129+
for _, w := range message {
130+
if s[w] {
131+
cnt++
132+
if cnt >= 2 {
133+
return true
134+
}
135+
}
136+
}
137+
return false
138+
}
139+
```
140+
141+
#### TypeScript
142+
143+
```ts
144+
function reportSpam(message: string[], bannedWords: string[]): boolean {
145+
const s = new Set<string>(bannedWords);
146+
let cnt = 0;
147+
for (const w of message) {
148+
if (s.has(w) && ++cnt >= 2) {
149+
return true;
150+
}
151+
}
152+
return false;
153+
}
154+
```
155+
156+
<!-- tabs:end -->
157+
158+
<!-- solution:end -->
159+
160+
<!-- problem:end -->
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3295.Report%20Spam%20Message/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3295. Report Spam Message](https://leetcode.com/problems/report-spam-message)
10+
11+
[中文文档](/solution/3200-3299/3295.Report%20Spam%20Message/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an array of strings <code>message</code> and an array of strings <code>bannedWords</code>.</p>
18+
19+
<p>An array of words is considered <strong>spam</strong> if there are <strong>at least</strong> two words in it that <b>exactly</b> match any word in <code>bannedWords</code>.</p>
20+
21+
<p>Return <code>true</code> if the array <code>message</code> is spam, and <code>false</code> otherwise.</p>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">message = [&quot;hello&quot;,&quot;world&quot;,&quot;leetcode&quot;], bannedWords = [&quot;world&quot;,&quot;hello&quot;]</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
32+
33+
<p>The words <code>&quot;hello&quot;</code> and <code>&quot;world&quot;</code> from the <code>message</code> array both appear in the <code>bannedWords</code> array.</p>
34+
</div>
35+
36+
<p><strong class="example">Example 2:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">message = [&quot;hello&quot;,&quot;programming&quot;,&quot;fun&quot;], bannedWords = [&quot;world&quot;,&quot;programming&quot;,&quot;leetcode&quot;]</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
42+
43+
<p><strong>Explanation:</strong></p>
44+
45+
<p>Only one word from the <code>message</code> array (<code>&quot;programming&quot;</code>) appears in the <code>bannedWords</code> array.</p>
46+
</div>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= message.length, bannedWords.length &lt;= 10<sup>5</sup></code></li>
53+
<li><code>1 &lt;= message[i].length, bannedWords[i].length &lt;= 15</code></li>
54+
<li><code>message[i]</code> and <code>bannedWords[i]</code> consist only of lowercase English letters.</li>
55+
</ul>
56+
57+
<!-- description:end -->
58+
59+
## Solutions
60+
61+
<!-- solution:start -->
62+
63+
### Solution 1: Hash Table
64+
65+
We use a hash table $s$ to store all the words in $\textit{bannedWords}$. Then, we traverse each word in $\textit{message}$. If the word appears in the hash table $s,ドル we increment the counter $cnt$ by one. If $cnt$ is greater than or equal to 2ドル,ドル we return $\text{true}$; otherwise, we return $\text{false}$.
66+
67+
The time complexity is $O((n + m) \times |w|),ドル and the space complexity is $O(m \times |w|)$. Here, $n$ is the length of the array $\textit{message},ドル and $m$ and $|w|$ are the length of the array $\textit{bannedWords}$ and the maximum length of the words in the array, respectively.
68+
69+
<!-- tabs:start -->
70+
71+
#### Python3
72+
73+
```python
74+
class Solution:
75+
def reportSpam(self, message: List[str], bannedWords: List[str]) -> bool:
76+
s = set(bannedWords)
77+
return sum(w in s for w in message) >= 2
78+
```
79+
80+
#### Java
81+
82+
```java
83+
class Solution {
84+
public boolean reportSpam(String[] message, String[] bannedWords) {
85+
Set<String> s = new HashSet<>();
86+
for (var w : bannedWords) {
87+
s.add(w);
88+
}
89+
int cnt = 0;
90+
for (var w : message) {
91+
if (s.contains(w) && ++cnt >= 2) {
92+
return true;
93+
}
94+
}
95+
return false;
96+
}
97+
}
98+
```
99+
100+
#### C++
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
bool reportSpam(vector<string>& message, vector<string>& bannedWords) {
106+
unordered_set<string> s(bannedWords.begin(), bannedWords.end());
107+
int cnt = 0;
108+
for (const auto& w : message) {
109+
if (s.contains(w) && ++cnt >= 2) {
110+
return true;
111+
}
112+
}
113+
return false;
114+
}
115+
};
116+
```
117+
118+
#### Go
119+
120+
```go
121+
func reportSpam(message []string, bannedWords []string) bool {
122+
s := map[string]bool{}
123+
for _, w := range bannedWords {
124+
s[w] = true
125+
}
126+
cnt := 0
127+
for _, w := range message {
128+
if s[w] {
129+
cnt++
130+
if cnt >= 2 {
131+
return true
132+
}
133+
}
134+
}
135+
return false
136+
}
137+
```
138+
139+
#### TypeScript
140+
141+
```ts
142+
function reportSpam(message: string[], bannedWords: string[]): boolean {
143+
const s = new Set<string>(bannedWords);
144+
let cnt = 0;
145+
for (const w of message) {
146+
if (s.has(w) && ++cnt >= 2) {
147+
return true;
148+
}
149+
}
150+
return false;
151+
}
152+
```
153+
154+
<!-- tabs:end -->
155+
156+
<!-- solution:end -->
157+
158+
<!-- problem:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool reportSpam(vector<string>& message, vector<string>& bannedWords) {
4+
unordered_set<string> s(bannedWords.begin(), bannedWords.end());
5+
int cnt = 0;
6+
for (const auto& w : message) {
7+
if (s.contains(w) && ++cnt >= 2) {
8+
return true;
9+
}
10+
}
11+
return false;
12+
}
13+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func reportSpam(message []string, bannedWords []string) bool {
2+
s := map[string]bool{}
3+
for _, w := range bannedWords {
4+
s[w] = true
5+
}
6+
cnt := 0
7+
for _, w := range message {
8+
if s[w] {
9+
cnt++
10+
if cnt >= 2 {
11+
return true
12+
}
13+
}
14+
}
15+
return false
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public boolean reportSpam(String[] message, String[] bannedWords) {
3+
Set<String> s = new HashSet<>();
4+
for (var w : bannedWords) {
5+
s.add(w);
6+
}
7+
int cnt = 0;
8+
for (var w : message) {
9+
if (s.contains(w) && ++cnt >= 2) {
10+
return true;
11+
}
12+
}
13+
return false;
14+
}
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def reportSpam(self, message: List[str], bannedWords: List[str]) -> bool:
3+
s = set(bannedWords)
4+
return sum(w in s for w in message) >= 2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function reportSpam(message: string[], bannedWords: string[]): boolean {
2+
const s = new Set<string>(bannedWords);
3+
let cnt = 0;
4+
for (const w of message) {
5+
if (s.has(w) && ++cnt >= 2) {
6+
return true;
7+
}
8+
}
9+
return false;
10+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /