原题链接在这里: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 Bb 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. 1 <= A.length, B.length <= 10000
  2. 1 <= A[i].length, B[i].length <= 10
  3. A[i] and B[i] consist only of lowercase letters.
  4. All words in A[i] are unique: there isn't i != j with A[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的更多相关文章

  1. [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 ...

  2. 【LeetCode】916. Word Subsets 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...

  3. 916. Word Subsets

    We are given two arrays A and B of words.  Each word is a string of lowercase letters. Now, say that ...

  4. 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 ...

  5. 【一天一道LeetCode】#90. Subsets II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  7. [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 ...

  8. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  9. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

随机推荐

  1. Scala2.12 从入门到精通实战高端视频课程(含网盘下载地址)

    Scala快速入门到精通 下载地址链接:https://pan.baidu.com/s/1bTSZSlWftFYaLQL6lVH62A 提取码:ohfk 下载后使用视频中自带的专用播放器打开视频就能看 ...

  2. C++强大背后

    转自MiloYip大神的博客 [原文]http://www.cnblogs.com/miloyip/archive/2010/09/17/behind_cplusplus.html 在31年前(197 ...

  3. Python如何进行有背景图片的界面跳转

    一.问题在进行tkinter点击按钮弹出新的界面的时候遇到了下面的问题:[_tkinter.TclError: image "pyimage2" doesn't exist] 原因 ...

  4. Oracel 数据库表操作

    表结构操作 创建表 create table tableName (id varchar2(36) primary key, name varchar2(36), age number(12,2), ...

  5. Github Markdown 图片如何并排显示

    Github Markdown 图片如何并排显示   要一张图片接着一张图片的写,中间不能有换行.如果换行的话则图片也换行 正确的写法: ![描述](图片链接)![描述](图片链接)![描述](图片链 ...

  6. 封装:WPF绘制曲线视图

    原文:封装:WPF绘制曲线视图 一.目的:绘制简单轻量级的曲线视图 二.实现: 1.动画加载曲线 2.点击图例显示隐藏对应曲线 3.绘制标准基准线 4.绘制蒙板显示标准区域 曲线图示例: 心电图示例: ...

  7. 转载 VUE+WebPack环境搭建 https://segmentfault.com/a/1190000010960666

    一.vue有两种使用方式: 1.下载vue.js <script src="vue.js"></script> 2.使用npm npm install vu ...

  8. spring Boot 学习(七、Spring Boot与开发热部署)

    一.热部署在开发中我们修改一个Java文件后想看到效果不得不重启应用,这导致大量时间 花费,我们希望不重启应用的情况下,程序可以自动部署(热部署).有以下四 种情况,如何能实现热部署.•1.模板引擎 ...

  9. systemd - CentOS 7进程守护&监控

    需求: 运行环境为CentOS 7系统,我们开发了一个程序,需要在开机时启动它,当程序进程crash或者开机之后,守护进程立即拉起进程. 解决方案: 使用CentOS 7中的init进程systemd ...

  10. Java 11 新特性介绍

    Java 11 已于 2018 年 9 月 25 日正式发布,之前在Java 10 新特性介绍中介绍过,为了加快的版本迭代.跟进社区反馈,Java 的版本发布周期调整为每六个月一次——即每半年发布一个 ...