Skip to content

Commit 3adb56b

Browse files
committedJan 13, 2022
Add solution #2085
1 parent 008a174 commit 3adb56b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
2016|[Maximum Difference Between Increasing Elements](./2016-maximum-difference-between-increasing-elements.js)|Easy|
246246
2047|[Number of Valid Words in a Sentence](./2047-number-of-valid-words-in-a-sentence.js)|Easy|
247247
2053|[Kth Distinct String in an Array](./2053-kth-distinct-string-in-an-array.js)|Medium|
248+
2085|[Count Common Words With One Occurrence](./2085-count-common-words-with-one-occurrence.js)|Easy|
248249
2095|[Delete the Middle Node of a Linked List](./2095-delete-the-middle-node-of-a-linked-list.js)|Medium|
249250
2114|[Maximum Number of Words Found in Sentences](./2114-maximum-number-of-words-found-in-sentences.js)|Easy|
250251

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 2085. Count Common Words With One Occurrence
3+
* https://leetcode.com/problems/count-common-words-with-one-occurrence/
4+
* Difficulty: Easy
5+
*
6+
* Given two string arrays words1 and words2, return the number of strings
7+
* that appear exactly once in each of the two arrays.
8+
*/
9+
10+
/**
11+
* @param {string[]} words1
12+
* @param {string[]} words2
13+
* @return {number}
14+
*/
15+
var countWords = function(words1, words2) {
16+
const map = new Map();
17+
words1.forEach(n => map.set(n, (map.get(n) || 0) + 1));
18+
[...map].forEach(([key, count]) => count !== 1 ? map.delete(key) : null);
19+
words2.forEach(n => map.has(n) ? map.set(n, (map.get(n) || 0) + 1) : null);
20+
21+
return [...map].filter(([, count]) => count === 2).length;
22+
};

0 commit comments

Comments
 (0)
Please sign in to comment.