|
| 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> </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 = ["hello","world","leetcode"], bannedWords = ["world","hello"]</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>"hello"</code> and <code>"world"</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 = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]</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>"programming"</code>) appears in the <code>bannedWords</code> array.</p> |
| 46 | +</div> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | +<p><strong>Constraints:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li><code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code></li> |
| 53 | + <li><code>1 <= message[i].length, bannedWords[i].length <= 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 --> |
0 commit comments