916. Word Subsets
We are given two arrays
A
andB
of words. Each word is a string of lowercase letters.Now, say that word
b
is a subset of worda
if every letter inb
occurs ina
, including multiplicity. For example,"wrr"
is a subset of"warrior"
, but is not a subset of"world"
.Now say a word
a
fromA
is universal if for everyb
inB
,b
is a subset ofa
.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]
andB[i]
consist only of lowercase letters.- All words in
A[i]
are unique: there isn'ti != j
withA[i] == A[j]
.
Approach #1: String. [Java]
class Solution {
public List<String> wordSubsets(String[] A, String[] B) {
int[] init = new int[26], temp;
for (String b : B) {
temp = counter(b);
for (int i = 0; i < 26; ++i) {
init[i] = Math.max(init[i], temp[i]);
}
}
List<String> ret = new ArrayList<>();
for (String a : A) {
temp = counter(a);
int i = 0;
for (i = 0; i < 26; ++i) {
if (temp[i] < init[i]) break;
}
if (i == 26) ret.add(a);
}
return ret;
} public int[] counter(String b) {
int[] temp = new int[26];
for (int i = 0; i < b.length(); ++i) {
temp[b.charAt(i)-'a']++;
}
return temp;
}
}
Reference:
https://leetcode.com/problems/word-subsets/discuss/175854/C%2B%2BJavaPython-Straight-Forward
916. Word Subsets的更多相关文章
- [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 ...
- LeetCode 916. Word Subsets
原题链接在这里:https://leetcode.com/problems/word-subsets/ 题目: We are given two arrays A and B of words. E ...
- 【LeetCode】916. Word Subsets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...
- [Swift]LeetCode916.单词子集 | Word Subsets
We are given two arrays A and B of words. Each word is a string of lowercase letters. Now, say that ...
- LeetCode - Word Subsets
We are given two arrays A and B of words. Each word is a string of lowercase letters. Now, say that ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
- delphi 数据导出到word
procedure TFrmWeekAnalysisQry.BtnExportToExcelClick(Sender: TObject);var wordApp,WordDoc,WrdSelectio ...
- 课程五(Sequence Models),第三周(Sequence models & Attention mechanism) —— 2.Programming assignments:Trigger word detection
Expected OutputTrigger Word Detection Welcome to the final programming assignment of this specializa ...
随机推荐
- JavaBugCharSequence
目录 文章背景 目录 问题分析 问题解决 说明 参考文章 版本记录 文章背景 新建一个项目时候,不知道为什么,代码出现java.lang.CharSequence cannot be resolved ...
- 理解Javascript的Prototype
在Javascript中创建对象主要分为三种方式 1. var catA = {name: "Fluffy", color: "White", age: 0}; ...
- 检测远程主机上的某个端口是否开启——telnet命令
要测试远程主机上的某个端口是否开启,无需使用太复杂的工作,windows下就自带了工具,那就是telnet.ping命令是不能检测端口,只能检测你和相应IP是否能连通. 1 安装telnet.win7 ...
- C++中的类型判断typeid()操作与java中的 instanceof 做比较
这是RTTI(运行阶段类型识别)的问题,c++有三个支持RTTI的元素: 1. dynamic_cast 操作符 如果可能的话,dynamic_cast操作符将使用一个指向基类的指针来生成一个 ...
- ES 遇到的一个坑too_many_clauses: maxClauseCount
异常: Caused by: org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: too_many_clauses: ...
- linux 磁盘 分区 MBR GPT
磁盘:由许多盘片,机械手臂,磁头和主轴马达所构成的,实际的数据都是写入盘片上面,而读写主要是通过机械手臂可伸展让磁头在盘片(在主轴马达的转动左右下,盘片跟着转动)上面进行读写操作.由于单一盘片写入数据 ...
- [LeetCode 题解]: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Label下FormattedText中的Span无法使用Binding的解决方法
在Xamarin.Forms中,Xaml的模板功能并没有原生WPF丰富,比如Label中虽然有FormattedText可以添加Span来丰富Label的功能,但是下面的Span中的Text并没有绑定 ...
- ES6—— iterator和for-of循环
Iterator 遍历器的作用:为各种数据结构,提供一个同意的,简便的访问接口.是的数据结构的成员能够按某种次序排列.ES6 新增了遍历命令 for...of 循环,Iterator接口主要供 for ...
- 如何使用jQuery + css3制作绚丽的网页经验总结
常见的网页特效有:轮播,滚动,闪烁,渐变,图标GIF效果,翻转,折叠,3D变换,主视觉等.以前没有CSS3时一些复杂的特效都要借助Flash来实现,Flash为什么会被淘汰,个人认为有以下几点: 1. ...