作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/word-subsets/description/

题目描述:

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

题目大意

如果B中的每一个元素中的每个字符都能在A中的某个字符串中找到,那么这个是个符合要求的字符串。求A中所有满足要求的字符串。

解题方法

如果按照题目要求的意思去直接做,那么要遍历B的每个元素的每个字符与A对应,这个时间复杂度肯定过不了OJ。所以,采用了一个很巧妙的方法,把B当做一个限制条件,直接求解这个限制条件。

对B的每个元素遍历,然后统计每个元素中每个字符串出现的次数,更新整体限制条件为每个元素在字符串中出现的次数的最大值。

统计结束之后,对A遍历的时候,只要看A是否满足这个限制条件就行了,所以挺快的。

时间复杂度是O(N),空间复杂度是O(N)。

class Solution(object):
def wordSubsets(self, A, B):
"""
:type A: List[str]
:type B: List[str]
:rtype: List[str]
"""
B = set(B)
res = []
count = collections.defaultdict(int)
for b in B:
cb = collections.Counter(b)
for c, v in cb.items():
count[c] = max(count[c], v)
res = []
for a in A:
ca = collections.Counter(a)
isSuccess = True
for c, v in count.items():
if v > ca[c]:
isSuccess = False
break
if isSuccess:
res.append(a)
return res

参考资料:

https://leetcode.com/problems/word-subsets/discuss/175854/C++JavaPython-Straight-Forward

日期

2018 年 9 月 30 日 —— 9月最后一天啦!

【LeetCode】916. Word Subsets 解题报告(Python)的更多相关文章

  1. 【LeetCode】78. Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

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

  3. LeetCode 916. Word Subsets

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

  4. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

随机推荐

  1. perl 获取目录信息

    1 #!/usr/bin/perl -w 2 use strict; 3 use FindBin qw($Bin $Script); 4 5 my $rp=$Bin; 6 print "th ...

  2. 利用plink软件基于LD信息过滤SNP

    最近有需求,对WGS测序获得SNP信息进行筛减,可问题是测序个体少,call rate,maf,hwe,等条件过滤后,snp数量还是千万级别,所以后面利用plink工具根据LD信息来滤除大量SNP标记 ...

  3. 35-Remove Element

    Remove Element My Submissions QuestionEditorial Solution Total Accepted: 115367 Total Submissions: 3 ...

  4. python 封装、绑定

    目录 python 封装.绑定 1.数据.方法的封装 2.隐藏属性 3.开放接口 4.绑定方法 1.对象的绑定 2.类的绑定(classmethod) 3.非绑定方法(staticmethod) 4. ...

  5. 求解线性递推方程第n项的一般方法

    概述 系数为常数,递推项系数均为一次的,形如下面形式的递推式,称为线性递推方程. \[f[n]=\begin{cases} C &n\in Value\\ a_1 f[n-1]+a_2 f[n ...

  6. WebRTC视频分辨率设置

    前面我们能够打开摄像头.getUserMedia()时会传入参数,在参数里我们可以指定宽高信息.通过宽高参数控制输出的视频分辨率. html 在页面上摆放一些元素,下面是主要部分 <div id ...

  7. 生产调优2 HDFS-集群压测

    目录 2 HDFS-集群压测 2.1 测试HDFS写性能 测试1 限制网络 1 向HDFS集群写10个128M的文件 测试结果分析 测试2 不限制网络 1 向HDFS集群写10个128M的文件 2 测 ...

  8. 日常Java 2021/10/24

    Java ArrrayList ArrayList类是一个可以动态修改的数组,没有固定大小的限制,可以在任何时候添加或者删除元素 ArrayList类在java.util包中使用之前需要引用 E:泛型 ...

  9. 大厂高频面试题Spring Bean生命周期最详解

    Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...

  10. Java、Scala获取Class实例

    Java获取Class实例的四种方式 package com.test; /** * @description: TODO * @author: HaoWu * @create: 2020/7/22 ...