class Solution {
public:
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
}
};
916. 单词子集
给你两个字符串数组 words1
和 words2
。
现在,如果 b
中的每个字母都出现在 a
中,包括重复出现的字母,那么称字符串 b
是字符串 a
的 子集 。
"wrr"
是 "warrior"
的子集,但不是 "world"
的子集。如果对 words2
中的每一个单词 b
,b
都是 a
的子集,那么我们称 words1
中的单词 a
是 通用单词 。
以数组形式返回 words1
中所有的通用单词。你可以按 任意顺序 返回答案。
示例 1:
输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"] 输出:["facebook","google","leetcode"]
示例 2:
输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"] 输出:["apple","google","leetcode"]
示例 3:
输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","oo"] 输出:["facebook","google"]
示例 4:
输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["lo","eo"] 输出:["google","leetcode"]
示例 5:
输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["ec","oc","ceo"] 输出:["facebook","leetcode"]
提示:
1 <= words1.length, words2.length <= 104
1 <= words1[i].length, words2[i].length <= 10
words1[i]
和 words2[i]
仅由小写英文字母组成words1
中的所有字符串 互不相同原站题解
golang 解法, 执行用时: 60 ms, 内存消耗: 7.1 MB, 提交时间: 2023-06-07 10:50:03
func wordSubsets(words1 []string, words2 []string) []string { record := make([]int,26) ans := make([]string,0) for _,i :=range words2{ tmp := make([]int,26) for _,j:=range i{ tmp[j-97] += 1 } for i:=0;i<26;i++{ record[i] = max(record[i],tmp[i]) } } for _,i := range words1{ if isValid(i,record){ ans = append(ans,i) } } return ans } func max(a,b int)int{ if a>b{ return a } return b } func isValid(arr string,b []int) bool{ a := make([]int,26) for _,j:=range arr{ a[j-97] += 1 } for i:=0;i<26;i++{ if b[i]>a[i]{ return false } } return true }
python3 解法, 执行用时: 800 ms, 内存消耗: 19.5 MB, 提交时间: 2023-06-07 10:48:59
''' 1.合B所有单词之力,统计26个字母的最高频次。 2.A中的a,要想符合,就必须26个字母每个字母的freq都超过B中的 ''' class Solution: def wordSubsets(self, A: List[str], B: List[str]) -> List[str]: B_char_freq = [0 for _ in range(26)] for b in B: tmp = self.func_count(b) for i in range(26): B_char_freq[i] = max(B_char_freq[i], tmp[i]) res = [] for a in A: tmp = self.func_count(a) #if all (x >= y for x,y in zip(tmp, B_char_freq)): if all (tmp[i] >= B_char_freq[i] for i in range(26)): res.append(a) return res def func_count(self, s: str) -> List[int]: char_freq = [0 for _ in range(26)] for c in s: char_freq[ord(c) - ord('a')] += 1 return char_freq
python3 解法, 执行用时: 744 ms, 内存消耗: 19.5 MB, 提交时间: 2023-06-07 10:47:14
class Solution: def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]: def count(word: str) -> str: ans = [0] * 26 for letter in word: ans[ord(letter) - ord('a')] += 1 return ans bmax = [0] * 26 for b in words2: for i, c in enumerate(count(b)): bmax[i] = max(bmax[i], c) ans = [] for a in words1: if all(x >= y for x, y in zip(count(a), bmax)): ans.append(a) return ans
java 解法, 执行用时: 15 ms, 内存消耗: 51.6 MB, 提交时间: 2023-06-07 10:45:12
// 将words2合并成一个单词 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; } }