|
3 | 3 | * https://leetcode.com/problems/word-subsets/
|
4 | 4 | * Difficulty: Medium
|
5 | 5 | *
|
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. |
7 | 7 | *
|
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. |
10 | 9 | *
|
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". |
12 | 11 | *
|
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. |
14 | 15 | */
|
15 | 16 |
|
16 | 17 | /**
|
17 |
| - * @param {string[]} A |
18 |
| - * @param {string[]} B |
| 18 | + * @param {string[]} words1 |
| 19 | + * @param {string[]} words2 |
19 | 20 | * @return {string[]}
|
20 | 21 | */
|
21 |
| -var wordSubsets = function(A, B) { |
| 22 | +var wordSubsets = function(words1, words2) { |
22 | 23 | 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) => { |
24 | 25 | b.split('').forEach(char => {
|
25 | 26 | map.set(char, (map.get(char) || 0) > count(b, char) ? map.get(char) : count(b, char));
|
26 | 27 | });
|
27 | 28 | return map;
|
28 | 29 | }, 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])); |
30 | 31 | };
|
0 commit comments