• [LeetCode] 916. 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 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 题目总结

  • 相关阅读:
    VMware安装Centos7超详细过程
    LVS+Keepalived小试牛刀
    Spring入门之AOP篇
    Spring学习笔记及资源
    Spring入门第一例
    Mybatis学习手记(二)
    Mybatis学习手记(一)
    java web 开发入门实例
    关于JAVA编译时找不到自定义包的问题
    Java 学习笔记及资源
  • 原文地址:https://www.cnblogs.com/cnoodle/p/16534716.html
Copyright © 2020-2023  润新知