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. 编码原则 之 Hollywood Principle

    原文 The Hollywood Principle states, “Don’t Call Us, We’ll Call You.” It’s closely related to the Depe ...

  2. 【Bilinear interpolation】双线性插值详解(转)

           最近在做视频拼接的项目,里面用到了图像的单应性矩阵变换,在最后的图像重映射,由于目标图像的坐标是非整数的,所以需要用到插值的方法,用的就是双线性插值,下面的博文主要是查看了前辈的博客对双 ...

  3. JavaScript(数据类型、字符串操作)

    JS基础 建议:一般情况下不在 head 标签中写 js 语句,因为该 js 语句会在 body 加载之前就执行,可能导致某些效果无效 // 单行注释 /*多行 * 注释*/ // 控制台输出语句 c ...

  4. Kotlin 基础语法

    Kotlin 文件以 .kt 为后缀. 包声明 代码文件的开头一般为包的声明: package com.runoob.main import java.util.* fun test() {} cla ...

  5. Introducing the Microservices Reference Architecture from NGINX

    Introducing the Microservices Reference Architecture from NGINX https://www.nginx.com/blog/introduci ...

  6. Shiro集成web环境[Springboot]-认证与授权

    Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...

  7. github命令总结

    一.创建版本库 1.创建一个空目录 $ mkdir learngit $ cd learngit $ pwd //pwd命令用于显示当前目录 2.创建一个版本库(通过git init命令把这个目录变成 ...

  8. Win10系列:C#应用控件进阶1

    线形 线形没有内部空间,若要呈现一条直线,需要用Line对象的Stroke和StrokeThickness 属性分别为其轮廓的颜色及轮廓的粗细赋值,若不设置这两个属性,线形将不会呈现.绘制一条线形图形 ...

  9. app性能测试指标

    性能测试在软件的质量保证中起着重要的作用,它包括的测试内容丰富多样.中国软件评测中心将性能测试概括为三个方面:应用在客户端性能的测试.应用在网络上性能的测试和应用在服务器端性能的测试.通常情况下,三方 ...

  10. 【转载】Oracle 中count(1) 、count(*) 和count(列名) 函数的区别

    1)count(1)与count(*)比较: 1.如果你的数据表没有主键,那么count(1)比count(*)快2.如果有主键的话,那主键(联合主键)作为count的条件也比count(*)要快3. ...