Skip to content

Commit 1490f3b

Browse files
authored
Create count-common-words-with-one-occurrence.cpp
1 parent 64b7157 commit 1490f3b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(m + n)
2+
// Space: O(m + n)
3+
4+
class Solution {
5+
public:
6+
int countWords(vector<string>& words1, vector<string>& words2) {
7+
unordered_map<string, int> cnt;
8+
for (const auto& w : words1) {
9+
++cnt[w];
10+
}
11+
for (const auto& w : words2) {
12+
if (cnt[w] < 2) {
13+
--cnt[w];
14+
}
15+
}
16+
int result = 0;
17+
for (const auto& [_, v] : cnt) {
18+
if (v == 0) {
19+
++result;
20+
}
21+
}
22+
return result;
23+
}
24+
};

0 commit comments

Comments
 (0)