File tree 2 files changed +23
-0
lines changed
2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change 245
245
2016|[ Maximum Difference Between Increasing Elements] ( ./2016-maximum-difference-between-increasing-elements.js ) |Easy|
246
246
2047|[ Number of Valid Words in a Sentence] ( ./2047-number-of-valid-words-in-a-sentence.js ) |Easy|
247
247
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|
248
249
2095|[ Delete the Middle Node of a Linked List] ( ./2095-delete-the-middle-node-of-a-linked-list.js ) |Medium|
249
250
2114|[ Maximum Number of Words Found in Sentences] ( ./2114-maximum-number-of-words-found-in-sentences.js ) |Easy|
250
251
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments