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 ...
随机推荐
- C++随机数笔记
版权声明:本文为CSDN博主「candyliuxj」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明. 原文链接:https://blog.csdn.net/can ...
- Linux crond任务调度(定时任务),Linux磁盘分区/挂载
一.crond任务调度 1.基本语法 crontab [选项] -e : 编辑 crontab定时任务 -l : 查询crontab -r : 删除当前用户所有的crontab任务 例子: 每分钟执行 ...
- 一个萝卜一个坑#我的C坑_两局变量
前面的笔记慢慢补 全局变量和局部变量的区别: 1.首字母 尽量不用全局变量原因: 1.占内存 2.降低通用性和可靠性 3.降清晰度 若在同一源文件中,全局变量和局部变量同名,记住很重要的一条:实参决定 ...
- Linux常用命令wc
wc名字来源: wc -- word, line, character, and byte count The wc utility displays the number of lines, wor ...
- k8s的学习
20191123 开始重头再学一遍k8 一 K8S的组件介绍
- Web api 右连接
这是原来的代码,两个表的连接的方式是inner join ,查不出我要的全部数据. 后来把代码稍稍改一下,就是left join join into 到一个临时对象里,相当于再select from ...
- ES6的新特性
ECMAScript 6(简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,又称ECMAScript 2015.ES6就是ES2015. 虽然目前并不是所有 ...
- 什么是MVC框架?
1.什么是mvc Model View Controller,是模型-视图-控制器的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到一个组件里,在改进和个性化 ...
- Java深入学习(2):并发队列
并发队列: 在并发队列中,JDK有两套实现: ConcurrentLinkedQueue:非阻塞式队列 BlockingQueue:阻塞式队列 阻塞式队列非阻塞式队列的区别: 阻塞式队列入列操作的时候 ...
- java并发值多线程同步业务场景以及解决方案
1.20个人排队同时访问2个购票窗口,同时能购票的只有两个人,当其中一个人买票完成后,18个人中的其中一个在占用窗口进行购买. 20个人相当于20个线程,2相当于资源,当18个人等待的时候,相当于线程 ...