Skip to content

Commit c835acc

Browse files
Sync LeetCode submission Runtime - 11 ms (65.22%), Memory - 21 MB (86.18%)
1 parent a717110 commit c835acc

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>Given two string arrays <code>words1</code> and <code>words2</code>, return <em>the number of strings that appear <strong>exactly once</strong> in <b>each</b>&nbsp;of the two arrays.</em></p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<pre>
7+
<strong>Input:</strong> words1 = [&quot;leetcode&quot;,&quot;is&quot;,&quot;amazing&quot;,&quot;as&quot;,&quot;is&quot;], words2 = [&quot;amazing&quot;,&quot;leetcode&quot;,&quot;is&quot;]
8+
<strong>Output:</strong> 2
9+
<strong>Explanation:</strong>
10+
- &quot;leetcode&quot; appears exactly once in each of the two arrays. We count this string.
11+
- &quot;amazing&quot; appears exactly once in each of the two arrays. We count this string.
12+
- &quot;is&quot; appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string.
13+
- &quot;as&quot; appears once in words1, but does not appear in words2. We do not count this string.
14+
Thus, there are 2 strings that appear exactly once in each of the two arrays.
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> words1 = [&quot;b&quot;,&quot;bb&quot;,&quot;bbb&quot;], words2 = [&quot;a&quot;,&quot;aa&quot;,&quot;aaa&quot;]
21+
<strong>Output:</strong> 0
22+
<strong>Explanation:</strong> There are no strings that appear in each of the two arrays.
23+
</pre>
24+
25+
<p><strong class="example">Example 3:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> words1 = [&quot;a&quot;,&quot;ab&quot;], words2 = [&quot;a&quot;,&quot;a&quot;,&quot;a&quot;,&quot;ab&quot;]
29+
<strong>Output:</strong> 1
30+
<strong>Explanation:</strong> The only string that appears exactly once in each of the two arrays is &quot;ab&quot;.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= words1.length, words2.length &lt;= 1000</code></li>
38+
<li><code>1 &lt;= words1[i].length, words2[j].length &lt;= 30</code></li>
39+
<li><code>words1[i]</code> and <code>words2[j]</code> consists only of lowercase English letters.</li>
40+
</ul>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int countWords(vector<string>& words1, vector<string>& words2) {
4+
unordered_map<string, pair<int, int>> freq;
5+
for (string& s : words1) freq[s].first++;
6+
for (string& s : words2) freq[s].second++;
7+
int ans = 0;
8+
for (auto& p : freq) ans += (p.second.first == 1 && p.second.second == 1);
9+
return ans;
10+
}
11+
};

0 commit comments

Comments
 (0)