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

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的更多相关文章

  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

    原题链接在这里:https://leetcode.com/problems/word-subsets/ 题目: We are given two arrays A and B of words.  E ...

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

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

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

  5. LeetCode - Word Subsets

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

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

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

  8. delphi 数据导出到word

    procedure TFrmWeekAnalysisQry.BtnExportToExcelClick(Sender: TObject);var wordApp,WordDoc,WrdSelectio ...

  9. 课程五(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 ...

随机推荐

  1. ubuntu 14.04 下配置 Go 1.51

    1.最简单的直接的方式(不用设置GOROOT) - 下载 归档文件 并解压到  /usr/local/go/  - 设置环境变量 - 设置系统级的 gedit /etc/profile # 在最末尾加 ...

  2. (1)WePHP 开启WePHP

    新建入口文件index.php,定义新项目的目录地址APP_PATH,引入WePHP项目入口文件 <?php define('APP_PATH','./index/'); require_onc ...

  3. Error creating bean with name 'us' defined in class path resource [com/liuyang/test/DI/beans2.xml]: Cannot resolve reference to bean 'daoa' while setting bean property 'daoa'; nested exception is org.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'us' defined ...

  4. kcp结构体字段含义

    conv 会话IDmtu 最大传输单元mss 最大分片大小state 连接状态(0xFFFFFFFF表示断开连接)snd_una 第一个未确认的包snd_nxt 下一个待分配的包的序号rcv_nxt ...

  5. Arch Linux freemind中文乱码

    原因:jre没有可用的中文字体 解决方法: (1) 安装中文字体,例如文泉驿微黑 pacman -S wqy-microhei (2) jre字体目录下建立fallback,并链接中文字体作为后备字体 ...

  6. .NET基础 (01).NET基础概念

    .NET基础概念 1 什么是CTS.CLS和CLR2 开发和运行.NET程序需要的最基本环节是什么3 .NET是否支持多编程语言开发4 CLR技术和COM技术的比较5 什么是程序集和应用程序域 1 什 ...

  7. select, iocp, epoll,kqueue及各种I/O复用机制

    http://blog.csdn.net/heyan1853/article/details/6457362 首先,介绍几种常见的I/O模型及其区别,如下: blocking I/O nonblock ...

  8. Java中的http(网络处理)相关的库:HttpClient,HttpCore(转载)

    [背景] 最近和之前,折腾了这个: [教程]模拟登陆百度之Java代码版 然后,对于Java的HttpClient,有了点了解. 现在整理如下: Java本身没有Http相关的库 Java本身,没有内 ...

  9. Code First ef SQL Server 版本不支持数据类型“datetime2”

    When calling DbContext.SaveChanges, I get a DbUpdateException:An unhandled exception of type 'System ...

  10. Transaction And Lock--死锁错误号1205

    在TSQL中,如果需要判断当前错误是否是因为死锁引起,可以使用ERROR_NUMBER()=1205来判断在C#中,使用SQLException来捕获 SQLException.Number=1205 ...