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. Linux Mint 17 搭建 JSP 环境

    一.配置Tomcat 服务器 1.下载 tomcat 2.解压后放到/usr/local目录下面 3.以root权限执行  chmod +x *.sh 4.启动 ./startup.sh#方式1 ./ ...

  2. Pandas -- Merge,join and concatenate

    Merge, join, and concatenate pandas provides various facilities for easily combining together Series ...

  3. CentOS下的Git服务器

    [Gitosis]CentOS下的Git服务器:Gitosis  [摘要]         详细介绍如何在CentOS上配置Gitosis        我们很多人知道Git可能是从Github开始的 ...

  4. HDU 4586 Play the Dice (数学,概率,等比公式,极限)

    题意:给你一个n面的骰子每个面有一个值,然后其中有不同值代表你能获得的钱,然后有m个特殊的面,当你骰到这一面的时候可以获得一个新的机会 问你能得到钱的期望. 析: 骰第一次     sum/n 骰第二 ...

  5. 创建EDM

    在学习linq过程中,我们难免会要创建EDM,这里简单的介绍一下EDM的创建过程 图示如下: 1.右击→添加→新建项→数据→Ado.net实体数据模型 选择适当的数据库,表后点击完成,vs中会自动生成 ...

  6. freePCRF免费版体验

    [摘要]遍寻网络数昼夜,未得开源PCRF,亦未得有参考价值的PCRF相关文档.所幸觅得免费体验版freePCRF软件.可窥见PCRF设计思路.方法:PCC规则定义.管理策略:遂记录安装.体验心得. f ...

  7. ssm集合的配置

    web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quo ...

  8. 1、Semantic-UI之开发环境搭建

    下载webstrom(其他的任何可以进行前端开发的软件都可以) Download WebStorm: The Smartest JavaScript IDE by JetBrains 安装并激活web ...

  9. 《T-SQL查询》- SQL逻辑处理

    下面列出SQL查询语句的一般形式,以及各个子句被逻辑处理的顺序步骤: (8) SELECT (9) DISTINCT (11) <TOP_specification> <select ...

  10. sql 的REPLACE

    REPLACE 用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法 REPLACE ( 'string_expression1' , 'string_expression2 ...