|
| 1 | +--- |
| 2 | +id: word-subsets |
| 3 | +title: Word Subsets |
| 4 | +sidebar_label: 0916 - Word Subsets |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Array |
| 8 | + - Hash Table |
| 9 | +description: "This is a solution to the Word Subsets problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +You are given two string arrays `words1` and `words2`. |
| 15 | + |
| 16 | +A string `b` is a **subset** of string `a` if every letter in `b` occurs in `a` including multiplicity. |
| 17 | + |
| 18 | +- For example, `"wrr"` is a subset of `"warrior"` but is not a subset of `"world"`. |
| 19 | + |
| 20 | +A string `a` from `words1` is **universal** if for every string `b` in `words2`, `b` is a subset of `a`. |
| 21 | + |
| 22 | +Return an array of all the **universal** strings in `words1`. You may return the answer in **any order**. |
| 23 | + |
| 24 | +### Examples |
| 25 | + |
| 26 | +**Example 1:** |
| 27 | + |
| 28 | +``` |
| 29 | +Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"] |
| 30 | +Output: ["facebook","google","leetcode"] |
| 31 | +``` |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | +``` |
| 35 | +Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"] |
| 36 | +Output: ["apple","google","leetcode"] |
| 37 | +``` |
| 38 | + |
| 39 | +### Constraints |
| 40 | + |
| 41 | +- $1 \leq words1.length, words2.length \leq 10^4$ |
| 42 | +- $1 \leq words1[i].length, words2[i].length \leq 10$ |
| 43 | +- `words1[i]` and `words2[i]` consist only of lowercase English letters. |
| 44 | +- All the strings of `words1` are **unique**. |
| 45 | + |
| 46 | +## Solution for Word Subsets |
| 47 | + |
| 48 | +## Approach: Reduce to Single Word in B |
| 49 | +### Intuition |
| 50 | + |
| 51 | +If b is a subset of a, then say a is a superset of b. Also, say $(word)N_{\text{"a"}}(\text{word})$ is the count of the number of $\text{"a"}$'s in the word. |
| 52 | + |
| 53 | +When we check whether a word `wordA` in `A` is a superset of `wordB`, we are individually checking the counts of letters: that for each $\text{letter}$, we have $N_{\text{letter}}(\text{wordA}) \geq N_{\text{letter}}(\text{wordB})$. |
| 54 | + |
| 55 | +Now, if we check whether a word `wordA` is a superset of all words $\text{wordB}_i$, we will check for each letter and each i, that $N_{\text{letter}}(\text{wordA}) \geq N_{\text{letter}}(\text{wordB}_i)$. This is the same as checking $N_{\text{letter}}(\text{wordA}) \geq \max\limits_i(N_{\text{letter}}(\text{wordB}_i))$. |
| 56 | + |
| 57 | +For example, when checking whether `"warrior"` is a superset of words B = `["wrr", "wa", "or"]`, we can combine these words in `B` to form a "maximum" word `"arrow"`, that has the maximum count of every letter in each word in `B`. |
| 58 | + |
| 59 | +### Algorithm |
| 60 | + |
| 61 | +Reduce `B` to a single word `bmax` as described above, then compare the counts of letters between words `a` in `A`, and `bmax`. |
| 62 | + |
| 63 | +### Code in Different Languages |
| 64 | + |
| 65 | +<Tabs> |
| 66 | +<TabItem value="cpp" label="C++"> |
| 67 | + <SolutionAuthor name="@Shreyash3087"/> |
| 68 | + |
| 69 | +```cpp |
| 70 | +#include <vector> |
| 71 | +#include <string> |
| 72 | +#include <unordered_map> |
| 73 | + |
| 74 | +class Solution { |
| 75 | +public: |
| 76 | + std::vector<std::string> wordSubsets(std::vector<std::string>& A, std::vector<std::string>& B) { |
| 77 | + std::vector<int> bmax(26, 0); |
| 78 | + for (const auto& b : B) { |
| 79 | + std::unordered_map<char, int> bCount = count(b); |
| 80 | + for (int i = 0; i < 26; ++i) |
| 81 | + bmax[i] = std::max(bmax[i], bCount[static_cast<char>('a' + i)]); |
| 82 | + } |
| 83 | + |
| 84 | + std::vector<std::string> ans; |
| 85 | + for (const auto& a : A) { |
| 86 | + std::unordered_map<char, int> aCount = count(a); |
| 87 | + bool universal = true; |
| 88 | + for (int i = 0; i < 26; ++i) { |
| 89 | + if (aCount[static_cast<char>('a' + i)] < bmax[i]) { |
| 90 | + universal = false; |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + if (universal) |
| 95 | + ans.push_back(a); |
| 96 | + } |
| 97 | + |
| 98 | + return ans; |
| 99 | + } |
| 100 | + |
| 101 | +private: |
| 102 | + std::unordered_map<char, int> count(const std::string& S) { |
| 103 | + std::unordered_map<char, int> ans; |
| 104 | + for (char c : S) |
| 105 | + ans[c]++; |
| 106 | + return ans; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | + |
| 111 | +``` |
| 112 | +</TabItem> |
| 113 | +<TabItem value="java" label="Java"> |
| 114 | + <SolutionAuthor name="@Shreyash3087"/> |
| 115 | +
|
| 116 | +```java |
| 117 | +class Solution { |
| 118 | + public List<String> wordSubsets(String[] A, String[] B) { |
| 119 | + int[] bmax = count(""); |
| 120 | + for (String b: B) { |
| 121 | + int[] bCount = count(b); |
| 122 | + for (int i = 0; i < 26; ++i) |
| 123 | + bmax[i] = Math.max(bmax[i], bCount[i]); |
| 124 | + } |
| 125 | +
|
| 126 | + List<String> ans = new ArrayList(); |
| 127 | + search: for (String a: A) { |
| 128 | + int[] aCount = count(a); |
| 129 | + for (int i = 0; i < 26; ++i) |
| 130 | + if (aCount[i] < bmax[i]) |
| 131 | + continue search; |
| 132 | + ans.add(a); |
| 133 | + } |
| 134 | +
|
| 135 | + return ans; |
| 136 | + } |
| 137 | +
|
| 138 | + public int[] count(String S) { |
| 139 | + int[] ans = new int[26]; |
| 140 | + for (char c: S.toCharArray()) |
| 141 | + ans[c - 'a']++; |
| 142 | + return ans; |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +</TabItem> |
| 148 | +<TabItem value="python" label="Python"> |
| 149 | + <SolutionAuthor name="@Shreyash3087"/> |
| 150 | + |
| 151 | +```python |
| 152 | +class Solution(object): |
| 153 | + def wordSubsets(self, A, B): |
| 154 | + def count(word): |
| 155 | + ans = [0] * 26 |
| 156 | + for letter in word: |
| 157 | + ans[ord(letter) - ord('a')] += 1 |
| 158 | + return ans |
| 159 | + |
| 160 | + bmax = [0] * 26 |
| 161 | + for b in B: |
| 162 | + for i, c in enumerate(count(b)): |
| 163 | + bmax[i] = max(bmax[i], c) |
| 164 | + |
| 165 | + ans = [] |
| 166 | + for a in A: |
| 167 | + if all(x >= y for x, y in zip(count(a), bmax)): |
| 168 | + ans.append(a) |
| 169 | + return ans |
| 170 | +``` |
| 171 | +</TabItem> |
| 172 | +</Tabs> |
| 173 | + |
| 174 | +### Complexity Analysis |
| 175 | + |
| 176 | +#### Time Complexity: $O(\mathcal{A}+\mathcal{B})$ |
| 177 | + |
| 178 | +> **Reason**: where $\mathcal{A}$ and $\mathcal{B}$ is the total amount of information in A and B respectively. |
| 179 | +
|
| 180 | +#### Space Complexity: $O(A.length+B.length)$ |
| 181 | + |
| 182 | +## Video Solution |
| 183 | + |
| 184 | +<LiteYouTubeEmbed |
| 185 | + id="ByQfvU8_fvM" |
| 186 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 187 | + title="Word Subsets | Live Coding with Explanation | Leetcode - 916" |
| 188 | + poster="hqdefault" |
| 189 | + webp /> |
| 190 | + |
| 191 | +## References |
| 192 | + |
| 193 | +- **LeetCode Problem**: [Word Subsets](https://leetcode.com/problems/word-subsets/description/) |
| 194 | + |
| 195 | +- **Solution Link**: [Word Subsets](https://leetcode.com/problems/word-subsets/solutions/) |
0 commit comments