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 ...
随机推荐
- Linux平台上常用到的c语言开发程序
Linux操作系统上大部分应用程序都是基于C语言开发的.小编将简单介绍Linux平台上常用的C语言开发程序. 一.C程序的结构1.函数 必须有一个且只能有一个主函数main(),主函数的名为main. ...
- js 数组sort, 多条件排序。
Array.sort(); sort()方法可以传入一个函数作为参数,然后依据该函数的逻辑,进行数组的排序. 一般用法:(数组元素从小大进行排序) var a = [9, 6, 5, 7, 11, 5 ...
- 重点|183道Java面试题可以说很详细了
<p style="text-align: right;"><span style="font-size: 14px;color: rgb(136, 1 ...
- C# SQl通过对视图数据二次查询,统计数据
问题描述: 原数据---------需要在原视图数据中,统计出每个Device_Num设备号下面的交易的总额和分别统计出微信支付宝的交易总额. 解决:从上图数据没办法使用直接查询出要求的数据. .1. ...
- C#读写修改设置调整UVC摄像头画面-清晰度
有时,我们需要在C#代码中对摄像头的清晰度进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄 ...
- WindowsXP序列号产生原理(椭圆曲线法)
WindowsXP序列号产生原理(椭圆曲线法) 来源 https://blog.csdn.net/zhiyuan411/article/details/5156330 参考 https://www. ...
- Java自学-日期 日期格式化
Java中使用SimpleDateFormat 进行日期格式化类 SimpleDateFormat 日期格式化类 示例 1 : 日期转字符串 y 代表年 M 代表月 d 代表日 H 代表24进制的小时 ...
- 查看Linux内核版本
您可能因多种原因需要确切知道GNU / Linux操作系统上运行的内核版本. 也许您正在调试与硬件相关的问题,或者了解影响旧内核版本的新安全漏洞,并且您想知道您的内核是否易受攻击. 无论是什么原因,从 ...
- Spring Data JPA的低级错误
//课程表 @Entity public class Class { @GeneratedValue(strategy = GenerationType.AUTO) @Id private Long ...
- Socket-网络服务提供的一种机制
网络编程 网络通信的要素 Ip,端口,协议(tcp/udp) 127.0.0.1 本机地址 默认主机名:localhost 端口号:用于标识进程的逻辑地址. 有效端口:0-65535 其中 ...