ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 916. Word Subsets

2022-07-30 12:31:49  阅读:165  来源: 互联网

标签:Word int 字母 words2 words1 单词 word 916 LeetCode


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 words2b 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"]

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.

单词子集。

给你两个字符串数组 words1 和 words2。

现在,如果 b 中的每个字母都出现在 a 中,包括重复出现的字母,那么称字符串 b 是字符串 a 的 子集 。

例如,"wrr" 是 "warrior" 的子集,但不是 "world" 的子集。
如果对 words2 中的每一个单词 b,b 都是 a 的子集,那么我们称 words1 中的单词 a 是 通用单词 。

以数组形式返回 words1 中所有的通用单词。你可以按 任意顺序 返回答案。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/word-subsets
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题算是 hashmap 和字符串的综合题。题目给了两个 list,分别装着一些单词,现在让我们返回的是 words1 中的通用单词。通用单词的定义简单说来就是每个字母的出现次数起码是大于等于 words2 中所有单词对应字母的出现次数的。

对于 words2 中的单词,我用一个长度为 26 的数组去统计每个单词的每个字母的出现次数,同时取最大值,记录每个字母最多出现过几次。这里我再用另一个数组 globalCount 把每个字母的最多出现次数放在一起,就是对 words1 中每个单词中字母出现次数的最低要求

接着我们再去遍历 words1 中的每个单词,记录每个字母的出现次数。如果某个字母的出现次数 < 这个字母在 globalCount 中的次数,就说明这个单词在当前这个字母上是不足以 cover words2中的所有单词的,所以他就不能被当做通用单词。

时间O(n) - 每个单词都要统计一下字母的出现次数,但是因为helper函数只需要看26次,所以这里权且视作O(1)的复杂度,则整体时间复杂度为O(n),n 是单词的个数

空间O(n)

Java实现

 1 class Solution {
 2     public List<String> wordSubsets(String[] words1, String[] words2) {
 3         int[] globalCount = new int[26];
 4         for (String word : words2) {
 5             int[] letterCount = helper(word);
 6             for (int i = 0; i < 26; i++) {
 7                 globalCount[i] = Math.max(globalCount[i], letterCount[i]);
 8             }
 9         }
10 
11         List<String> res = new ArrayList<>();
12         for (String word : words1) {
13             int[] count = helper(word);
14             boolean ok = true;
15             for (int i = 0; i < 26; i++) {
16                 if (count[i] < globalCount[i]) {
17                     ok = false;
18                     break;
19                 }
20             }
21             if (ok) res.add(word);
22         }
23         return res;
24     }
25 
26     private int[] helper(String word) {
27         int[] letterCount = new int[26];
28         for (char c : word.toCharArray()) {
29             letterCount[c - 'a']++;
30         }
31         return letterCount;
32     }
33 }

 

LeetCode 题目总结

标签:Word,int,字母,words2,words1,单词,word,916,LeetCode
来源: https://www.cnblogs.com/cnoodle/p/16534716.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有