LeetCode 916. Word Subsets
原题链接在这里:https://leetcode.com/problems/word-subsets/
题目:
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]
andB[i]
consist only of lowercase letters.- All words in
A[i]
are unique: there isn'ti != j
withA[i] == A[j]
.
题解:
String a in A, if it is universal for every b in B, it must cover all the letters and max corresponding multiplicity in B.
Thus construct a map to maintain all letters and max corresponding multiplicity in B.
Then for each A, if it could cover this map, then add it to the res.
Time Complexity: O(m*n + p*q). m = A.length. n = average length of string in A. p = B.length. q = average length of string in B.
Space: O(1).
AC Java:
class Solution {
public List<String> wordSubsets(String[] A, String[] B) {
List<String> res = new ArrayList<>();
if(A == null || A.length == 0){
return res;
} if(B == null || B.length == 0){
return Arrays.asList(A);
} int [] map = new int[26];
for(String b : B){
int [] bMap = new int[26];
for(char c : b.toCharArray()){
bMap[c - 'a']++;
} for(int i = 0; i<26; i++){
map[i] = Math.max(map[i], bMap[i]);
}
} for(String a : A){
int [] aMap = new int[26];
for(char c : a.toCharArray()){
aMap[c-'a']++;
} boolean isNotSubSet = false;
for(int i = 0; i<26; i++){
if(aMap[i] < map[i]){
isNotSubSet = true;
break;
}
} if(!isNotSubSet){
res.add(a);
}
} return res;
}
}
LeetCode 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...
- 916. Word Subsets
We are given two arrays A and B of words. Each word is a string of lowercase letters. Now, say that ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【一天一道LeetCode】#90. Subsets II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode] 79. Word Search 单词搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
随机推荐
- 超级简单POI多sheet导出Excel实战
本章节主要基于上一章节单sheet导出的基础上进行改造实现多sheet的导出,上一章节参考地址:https://www.cnblogs.com/sunny1009/p/11437005.html 1. ...
- HDU校赛 | 2019 Multi-University Training Contest 5
2019 Multi-University Training Contest 5 http://acm.hdu.edu.cn/contests/contest_show.php?cid=852 100 ...
- Java中守护线程的总结 thread.setDaemon(true)
https://www.cnblogs.com/ziq711/p/8228255.html 在Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 用个比较 ...
- Android 权限的一些细节
Android 权限的一些细节 1 哪些app属于system app?为了区分privilege app和system app,这里先说明system app是什么,避免之后的讨论概念混乱. 在Pa ...
- 如何在ppt全屏演示时仍然显示任务栏?
相信做过ppt演讲的人会有这样的体会:有的时候希望全屏ppt时不要直接霸占全部的屏幕,至少希望能够看到任务栏,这样就可以仍然方便切换程序. 如何实现呢? 很简单,看下图吧:) https://www. ...
- HTML5 新增文本标签
一.mark 标记文本 <mark> 标签定义带有记号的文本,表示页面中需要突出显示或高亮显示的信息. 通常在引用原文的时候使用 mark 元素,目的是引起当前用户的注意. 语法格式: & ...
- Android中自定义水球
如图所示: 自定义属性: 在values下创建attrs.xml 文件 <?xml version="1.0" encoding="utf-8"?> ...
- git的基本使用和多人协作合并管理
1.代码版本控制工具 git 分布式 svn 集中式 2.配置git 配置用户名以及邮箱账号,用于记录用户信息 git config --global user.name 'wudaxun' git ...
- mysql float和decimal
结论: 1. float 默认只保存6位(除去小数点),如果超过6位,则四舍五入,所以float存储的数据是不精确的,只是近似值: 2. decimal,如果输入的数据超过了定义的最大值,那么则溢出, ...
- linux/unix发行清单
unix http://www.slackware.com/ https://www.freebsd.org/ http://www.netbsd.org/ https://www.opensuse. ...