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. 线性回归(linear regression)

    基本形式 最小二乘法估计拟合参数 最小二乘法:基于均方误差最小化来进行模型求解的方法称为“最小二乘法”(least square method) 即(左边代表 $\mathbf{\omega }$ 和 ...

  2. _quest_random

    -- 随机任务-- 可以实现玩家随机获取任务-- 小技巧:需要控制物品法防,在_function_menu表配置物品indexID为28 `comment`备注 `questId`任务ID `chan ...

  3. 查找IDEA 项目中的依赖包存放在.m2位置

    原因:在maven项目pom.xml中添加依赖,可能由于网络不好,下载不完全,导致再次下载一直报错. 就会导致出现依赖一直报错.

  4. HTTP请求处理流程 MVC核心(MVC就是扩展了一个HttpModule)

    访问Localhost:8080/Home/index.aspx 在调用MVC扩展的UrlRoutingModule的时候  会先检查物理路径文件是否存在  存在的话就不执行MVC中的路由匹配规则  ...

  5. 一起用ipython

    安装 安装python2版本的软件包就用命令 pip install ipython 安装python3版本对应的软件包就用命令 pip3 install ipython 进入了ipython,ipy ...

  6. 【文献07】基于MPC的WMR点镇定-极坐标系下和轨迹跟踪

    参考: Kuhne F , Lages W , Silva J D . Point stabilization of mobile robots with nonlinear model predic ...

  7. 连手机logcat,出现read:unexpected EOF

    使用logcat时,出现: 网上搜原因解释为log太多,普遍的解决方法是: adb logcat -G 20m 根本解决方法推荐:开发者设置,增大log size

  8. 238. Product of Array Except Self除自身以外数组的乘积

    网址:https://leetcode.com/problems/product-of-array-except-self/ 参考:https://leetcode.com/problems/prod ...

  9. MapReduce(五)

    MapReduce的(五) 1.MapReduce的多表关联查询. 根据文本数据格式.查询多个文本中的内容关联.查询. 2.MapReduce的多任务窜执行的使用 多任务的串联执行问题,主要是要建立c ...

  10. kafka consumer 指定 offset,进行消息回溯

    kafka consumer 如何根据 offset,进行消息回溯?下面的文档给出了 demo: https://cwiki.apache.org/confluence/display/KAFKA/0 ...