We are given two arrays A and B of words.  Each word is a string of lowercase letters.

Now, say that word b is a subset of word 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".

Now say a word a from A is universal if for every b in B, b is a subset of a. 

Return a list of all universal words in A.  You can return the words in any order.

Example 1:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","o"]
Output: ["facebook","google","leetcode"]
Example 2: Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["l","e"]
Output: ["apple","google","leetcode"]
Example 3: Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","oo"]
Output: ["facebook","google"]
Example 4: Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["lo","eo"]
Output: ["google","leetcode"]
Example 5: Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["ec","oc","ceo"]
Output: ["facebook","leetcode"] Note: 1 <= A.length, B.length <= 10000
1 <= A[i].length, B[i].length <= 10
A[i] and B[i] consist only of lowercase letters.
All words in A[i] are unique: there isn't i != j with A[i] == A[j].

这道题的核心就是理解对于B的处理:

先来理解题目,题目强调的是单个字母,不存在字母顺序问题,所以只要统计出B中每个小写字母,在所有的单词中出现次数最高的那个数(例如,B = ["loo","eo"],那么o 最大的出现次数为2, 因为在"loo"出现了两次,在"eo"出现了一次,所以以最大的为准),然后再统计A中的每个单词中的每个字母出现的次数,与其对比即可。
具体思路:
(1)先统计出数组B中每个小写字母的最大出现次数,因为是题目只包含26个小写字母,所以,可以直接转换成数组来保存。(这这个过程中,可以用字典或者hash来保存,这样可能会更快一些)
(2)然后,依次统计A中的没单词中每个字母的出现个数,与(1)统计的结果做对比,选择出通用单词即可。
原文:https://blog.csdn.net/xx_123_1_rj/article/details/82992861

class Solution {
public List<String> wordSubsets(String[] A, String[] B) {
List<String> resList = new ArrayList<>();
int[] temp = null;
if(A == null || B == null || A.length == 0 || B.length == 0){
return resList;
}
int[] max = new int[26];
for(String b : B){
temp = new int[26];
for(char c : b.toCharArray()){
temp[c-'a']++;
}
for(int i = 0; i< 26; i++){
if(temp[i] > max[i]){
max[i] = temp[i];
}
}
}
for(String a : A){
temp = new int[26];
for(char c : a.toCharArray()){
temp[c-'a']++;
}
for(int i = 0; i< 26; i++){
if(temp[i] < max[i]){
break;
}
if(i == 25){
resList.add(a);
}
}
}
return resList;
} }

LeetCode - Word Subsets的更多相关文章

  1. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  2. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  3. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  5. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  6. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  7. [LeetCode] 916. Word Subsets 单词子集合

    We are given two arrays A and B of words.  Each word is a string of lowercase letters. Now, say that ...

  8. LeetCode 916. Word Subsets

    原题链接在这里:https://leetcode.com/problems/word-subsets/ 题目: We are given two arrays A and B of words.  E ...

  9. 【LeetCode】916. Word Subsets 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...

随机推荐

  1. pycharm 倒入request包方法(新手)

    1.先安装request模块,在pycharm import,但是怎么也倒不进去,咨询了开发,原来需要把包倒入到pycharm 编译器里面才可以import 成功,具体操作步骤如下 首先确认下自己电脑 ...

  2. 广工十四届校赛 count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ...

  3. 『TensorFlow』卷积层、池化层详解

    一.前向计算和反向传播数学过程讲解

  4. Maven入门介绍

    一.Maven的基本概念 1.1为什么需要Maven(作用) Ⅰ. 大家都知道使用Maven,那么我们为什么要要使用maven大家思考过吗?其实我也只是对maven入门阶段,刚刚接触的时候只是知道使用 ...

  5. arrow function、function.apply

    An arrow function expression has a shorter syntax than a function expression and does not have its o ...

  6. linux搭建node环境

    这篇完全够了! 地址:https://www.cnblogs.com/lovefc/p/8847343.html 附上一张图:

  7. hadoop集群中动态添加节点

    集群的性能问题需要增加服务器节点以提高整体性能 https://www.cnblogs.com/fefjay/p/6048269.html hadoop集群之间hdfs文件复制 https://www ...

  8. webForm TO MVC

     

  9. 正常终止expdp作业

    1.先查询expdp对应的job_nameSQL> select * from dba_datapump_jobs; OWNER_NAME JOB_NAME  OPERATION JOB_MOD ...

  10. Python _Mix*9

    1. 函数 函数是对功能的封装 语法: def 函数名(形参列表): 函数体(代码块) 代码块中有可能包含return 调用: 函数名(实参列表) def mix(a,b): #def 函数名(a和b ...