Skip to content

Commit 52f12e6

Browse files
committed
Sync LeetCode submission Runtime - 507 ms (34.66%), Memory - 21 MB (45.02%)
1 parent a9905f5 commit 52f12e6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

0952-word-subsets/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>You are given two string arrays <code>words1</code> and <code>words2</code>.</p>
2+
3+
<p>A string <code>b</code> is a <strong>subset</strong> of string <code>a</code> if every letter in <code>b</code> occurs in <code>a</code> including multiplicity.</p>
4+
5+
<ul>
6+
<li>For example, <code>&quot;wrr&quot;</code> is a subset of <code>&quot;warrior&quot;</code> but is not a subset of <code>&quot;world&quot;</code>.</li>
7+
</ul>
8+
9+
<p>A string <code>a</code> from <code>words1</code> is <strong>universal</strong> if for every string <code>b</code> in <code>words2</code>, <code>b</code> is a subset of <code>a</code>.</p>
10+
11+
<p>Return an array of all the <strong>universal</strong> strings in <code>words1</code>. You may return the answer in <strong>any order</strong>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> words1 = [&quot;amazon&quot;,&quot;apple&quot;,&quot;facebook&quot;,&quot;google&quot;,&quot;leetcode&quot;], words2 = [&quot;e&quot;,&quot;o&quot;]
18+
<strong>Output:</strong> [&quot;facebook&quot;,&quot;google&quot;,&quot;leetcode&quot;]
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> words1 = [&quot;amazon&quot;,&quot;apple&quot;,&quot;facebook&quot;,&quot;google&quot;,&quot;leetcode&quot;], words2 = [&quot;l&quot;,&quot;e&quot;]
25+
<strong>Output:</strong> [&quot;apple&quot;,&quot;google&quot;,&quot;leetcode&quot;]
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= words1.length, words2.length &lt;= 10<sup>4</sup></code></li>
33+
<li><code>1 &lt;= words1[i].length, words2[i].length &lt;= 10</code></li>
34+
<li><code>words1[i]</code> and <code>words2[i]</code> consist only of lowercase English letters.</li>
35+
<li>All the strings of <code>words1</code> are <strong>unique</strong>.</li>
36+
</ul>

0952-word-subsets/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Approach 1: Reduce to Single Word in B
2+
3+
# A = len(words1) ,B = len(words2), and W is max word length
4+
# Time: O(A * W + B * W)
5+
# Space: O(A + B) -> O(A)
6+
7+
class Solution:
8+
def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
9+
def count(word):
10+
ans = [0] * 26
11+
for letter in word:
12+
ans[ord(letter) - ord('a')] += 1
13+
return ans
14+
15+
bmax = [0] * 26
16+
for b in words2:
17+
for i, c in enumerate(count(b)):
18+
bmax[i] = max(bmax[i], c)
19+
20+
ans = []
21+
for a in words1:
22+
if all(x >= y for x, y in zip(count(a), bmax)):
23+
ans.append(a)
24+
25+
return ans
26+

0 commit comments

Comments
 (0)