加载中...
916-单词子集(Word Subsets)
发表于:2021-12-03 | 分类: 中等
字数统计: 1.2k | 阅读时长: 6分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/word-subsets

英文原文

You are given two string arrays words1 and words2.

A string b is a subset of string a if every letter in b occurs in a including multiplicity.

  • For example, "wrr" is a subset of "warrior" but is not a subset of "world".

A string a from words1 is universal if for every string b in words2, b is a subset of a.

Return an array of all the universal strings in words1. You may return the answer in any order.

 

Example 1:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
Output: ["facebook","google","leetcode"]

Example 2:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
Output: ["apple","google","leetcode"]

Example 3:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","oo"]
Output: ["facebook","google"]

Example 4:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["lo","eo"]
Output: ["google","leetcode"]

Example 5:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["ec","oc","ceo"]
Output: ["facebook","leetcode"]

 

Constraints:

  • 1 <= words1.length, words2.length <= 104
  • 1 <= words1[i].length, words2[i].length <= 10
  • words1[i] and words2[i] consist only of lowercase English letters.
  • All the strings of words1 are unique.

中文题目

我们给出两个单词数组 A 和 B。每个单词都是一串小写字母。

现在,如果 b 中的每个字母都出现在 a 中,包括重复出现的字母,那么称单词 b 是单词 a 的子集。 例如,“wrr” 是 “warrior” 的子集,但不是 “world” 的子集。

如果对 B 中的每一个单词 bb 都是 a 的子集,那么我们称 A 中的单词 a通用的

你可以按任意顺序以列表形式返回 A 中所有的通用单词。

 

示例 1:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["e","o"]
输出:["facebook","google","leetcode"]

示例 2:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["l","e"]
输出:["apple","google","leetcode"]

示例 3:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["e","oo"]
输出:["facebook","google"]

示例 4:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["lo","eo"]
输出:["google","leetcode"]

示例 5:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["ec","oc","ceo"]
输出:["facebook","leetcode"]

 

提示:

  1. 1 <= A.length, B.length <= 10000
  2. 1 <= A[i].length, B[i].length <= 10
  3. A[i] 和 B[i] 只由小写字母组成。
  4. A[i] 中所有的单词都是独一无二的,也就是说不存在 i != j 使得 A[i] == A[j]

通过代码

官方题解

方法 1:将 B 合并成一个单词

想法

如果 ba 的子集,那么就称 ab 的超集。记录 $N_{\text{“a”}}(\text{word})$ 是 word 中字母 $\text{“a”}$ 出现次数。

当我们检查 A 中的单词 wordA 是否是 wordB 的超集时,我们只需要单独检验每个字母个数:对于每个字母,有 $N_{\text{letter}}(\text{wordA}) \geq N_{\text{letter}}(\text{wordB})$。

现在,检验单词 wordA 是否是所有 $\text{wordB}i$ 的超集,我们需要检验所有 $i$ 是否满足 $N{\text{letter}}(\text{wordA}) \geq N_{\text{letter}}(\text{wordB}i)$,等价于检验 $N{\text{letter}}(\text{wordA}) \geq \max\limits_i(N_{\text{letter}}(\text{wordB}_i))$。

例如,当我们检验 "warrior" 是否是 B = ["wrr", "wa", "or"] 的超集时,我们可以按照字母出现的最多次数将 B 中所有单词合并成一个单词 "arrow",然后判断一次即可。

算法

B 合并成一个单独的单词 bmax,然后比较 A 中的所有单词 a

[]
class Solution { public List<String> wordSubsets(String[] A, String[] B) { int[] bmax = count(""); for (String b: B) { int[] bCount = count(b); for (int i = 0; i < 26; ++i) bmax[i] = Math.max(bmax[i], bCount[i]); } List<String> ans = new ArrayList(); search: for (String a: A) { int[] aCount = count(a); for (int i = 0; i < 26; ++i) if (aCount[i] < bmax[i]) continue search; ans.add(a); } return ans; } public int[] count(String S) { int[] ans = new int[26]; for (char c: S.toCharArray()) ans[c - 'a']++; return ans; } }
[]
class Solution(object): def wordSubsets(self, A, B): def count(word): ans = [0] * 26 for letter in word: ans[ord(letter) - ord('a')] += 1 return ans bmax = [0] * 26 for b in B: for i, c in enumerate(count(b)): bmax[i] = max(bmax[i], c) ans = [] for a in A: if all(x >= y for x, y in zip(count(a), bmax)): ans.append(a) return ans

复杂度分析

  • 时间复杂度:$O(A+B)$,其中 $A$ 和 $B$ 分别是 AB 的单词个数。
  • 空间复杂度:$O(A\text{.length} + B\text{.length})$。

统计信息

通过次数 提交次数 AC比率
6659 14936 44.6%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
915-分割数组(Partition Array into Disjoint Intervals)
下一篇:
917-仅仅反转字母(Reverse Only Letters)
本文目录
本文目录