博客
关于我
916. Word Subsets
阅读量:414 次
发布时间:2019-03-06

本文共 2056 字,大约阅读时间需要 6 分钟。

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

 

转载地址:http://gftuz.baihongyu.com/

你可能感兴趣的文章
SpringBoot中关于Mybatis使用的三个问题
查看>>
MapReduce实验
查看>>
Leaflet 带箭头轨迹以及沿轨迹带方向的动态marker
查看>>
java大数据最全课程学习笔记(1)--Hadoop简介和安装及伪分布式
查看>>
java大数据最全课程学习笔记(2)--Hadoop完全分布式运行模式
查看>>
大部分程序员还不知道的 Servelt3 异步请求,原来这么简单?
查看>>
[apue] popen/pclose 疑点解惑
查看>>
[apue] getopt 可能重排参数
查看>>
移动互联网恶意软件命名及分类
查看>>
adb shell am 的用法
查看>>
PySide图形界面开发(一)
查看>>
Android如果有一个任意写入的漏洞,如何将写权限转成执行权限
查看>>
三角网格体积计算
查看>>
现代3D图形编程学习-基础简介(2) (译)
查看>>
Github教程(3)
查看>>
vue实现简单的点击切换颜色
查看>>
vue3 template refs dom的引用、组件的引用、获取子组件的值
查看>>
深入浅出mybatis
查看>>
Zookeeper快速开始
查看>>
882. Reachable Nodes In Subdivided Graph
查看>>