Skip to content

Commit e883319

Browse files
committed
Add solution #916
1 parent 8a0a09a commit e883319

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

solutions/0916-word-subsets.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33
* https://leetcode.com/problems/word-subsets/
44
* Difficulty: Medium
55
*
6-
* We are given two arrays A and B of words. Each word is a string of lowercase letters.
6+
* You are given two string arrays words1 and words2.
77
*
8-
* Now, say that word b is a subset of word a if every letter in b occurs in a, including
9-
* multiplicity. For example, "wrr" is a subset of "warrior", but is not a subset of "world".
8+
* A string b is a subset of string a if every letter in b occurs in a including multiplicity.
109
*
11-
* Now say a word a from A is universal if for every b in B, b is a subset of a.
10+
* For example, "wrr" is a subset of "warrior" but is not a subset of "world".
1211
*
13-
* Return a list of all universal words in A. You can return the words in any order.
12+
* A string a from words1 is universal if for every string b in words2, b is a subset of a.
13+
*
14+
* Return an array of all the universal strings in words1. You may return the answer in any order.
1415
*/
1516

1617
/**
17-
* @param {string[]} A
18-
* @param {string[]} B
18+
* @param {string[]} words1
19+
* @param {string[]} words2
1920
* @return {string[]}
2021
*/
21-
var wordSubsets = function(A, B) {
22+
var wordSubsets = function(words1, words2) {
2223
const count = (string, char) => string.split(char).length - 1;
23-
const subset = Array.from(B.reduce((map, b) => {
24+
const subset = Array.from(words2.reduce((map, b) => {
2425
b.split('').forEach(char => {
2526
map.set(char, (map.get(char) || 0) > count(b, char) ? map.get(char) : count(b, char));
2627
});
2728
return map;
2829
}, new Map()));
29-
return A.filter(a => subset.every(match => count(a, match[0]) >= match[1]));
30+
return words1.filter(a => subset.every(match => count(a, match[0]) >= match[1]));
3031
};

0 commit comments

Comments
 (0)