[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 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 <= A.length, B.length <= 10000
1 <= A[i].length, B[i].length <= 10
A[i]
andB[i]
consist only of lowercase letters.- All words in
A[i]
are unique: there isn'ti != j
withA[i] == A[j]
.
这道题定义了两个单词之间的一种子集合关系,就是说假如单词b中的每个字母都在单词a中出现了(包括重复字母),就说单词b是单词a的子集合。现在给了两个单词集合A和B,让找出集合A中的所有满足要求的单词,使得集合B中的所有单词都是其子集合。配合上题目中给的一堆例子,意思并不难理解,根据子集合的定义关系,其实就是说若单词a中的每个字母的出现次数都大于等于单词b中每个字母的出现次数,单词b就一定是a的子集合。现在由于集合B中的所有单词都必须是A中某个单词的子集合,那么其实只要对于每个字母,都统计出集合B中某个单词中出现的最大次数,比如对于这个例子,B=["eo","oo"],其中e最多出现1次,而o最多出现2次,那么只要集合A中有单词的e出现不少1次,o出现不少于2次,则集合B中的所有单词一定都是其子集合。这就是本题的解题思路,这里使用一个大小为 26 的一维数组 charCnt 来统计集合B中每个字母的最大出现次数,而将统计每个单词的字母次数的操作放到一个子函数 helper 中,当 charCnt 数组更新完毕后,下面就开始检验集合A中的所有单词了。对于每个遍历到的单词,还是要先统计其每个字母的出现次数,然后跟 charCnt 中每个位置上的数字比较,只要均大于等于 charCnt 中的数字,就可以加入到结果 res 中了,参见代码如下:
class Solution {
public:
vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
vector<string> res;
vector<int> charCnt(26);
for (string &b : B) {
vector<int> t = helper(b);
for (int i = 0; i < 26; ++i) {
charCnt[i] = max(charCnt[i], t[i]);
}
}
for (string &a : A) {
vector<int> t = helper(a);
int i = 0;
for (; i < 26; ++i) {
if (t[i] < charCnt[i]) break;
}
if (i == 26) res.push_back(a);
}
return res;
}
vector<int> helper(string& word) {
vector<int> res(26);
for (char c : word) ++res[c - 'a'];
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/916
参考资料:
https://leetcode.com/problems/word-subsets/
https://leetcode.com/problems/word-subsets/discuss/175854/C%2B%2BJavaPython-Straight-Forward
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 916. Word Subsets 单词子集合的更多相关文章
- LeetCode 916. Word Subsets
原题链接在这里:https://leetcode.com/problems/word-subsets/ 题目: We are given two arrays A and B of words. E ...
- LeetCode 78. Subsets(子集合)
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [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 ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- 【LeetCode】916. Word Subsets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...
随机推荐
- 1+X证书Web前端开发规范手册
一.规范目的 1.1 概述 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进行前台页面开发. 本 ...
- OneNote: 一站式笔记管理平台
- Spring Boot整合Mybatis配置详解
首先,你得有个Spring Boot项目. 平时开发常用的repository包在mybatis里被替换成了mapper. 配置: 1.引入依赖: <dependency> <gro ...
- .NET Core 学习笔记
1. System.Composition – Using Import Attributes 截图: 2. System.Composition – Using Reflection 截图: 以后会 ...
- Spring整合Mybaits java.sql.SQLException: Access denied for user '***'@'localhost' (using password: YES)
最近在搞Spring和Mybatis的整合,当我们在Spring里面配置数据源,而数据源是从外部的properties文件读取过来的时候就会报错 java.sql.SQLException: Acce ...
- C++调用linux命令并获取返回值
qt中封装了相关的方法, 但是因为我的命令中用到了管道命令, 出现了非预期结果, 所有改用了linux系统原生的方法. 下边是一个判断某进程是否存在的例子. 当前存在一个问题,当linux返回多行时, ...
- Docker Desktop for Windows 安装步骤
Docker Desktop for Windows 安装要求 Docker Desktop for Windows需要运行Microsoft Hyper-V.如果需要,Docker Desktop ...
- ORM:对象关系映射
一.简单操作 定义:面向对象和关系型数据库的一种映射,通过操作对象的方式操作数据 对应关系: 类对应数据表 对象对应数据行(记录) 属性对应字段 导入:from app01 import models ...
- ios 点击webview获取图片url (js交互)
加手势 -(void)handleSingleTap:(UITapGestureRecognizer *)sender { CGPoint pt = [sender locationInView:_c ...
- 第3篇-超市管理系统Scrum冲刺博客
一.站立式会议: 1.会议照片 2.昨天完成的工作 ①数据库方面:根据需求关系为在数据库中建立相关表的基本模型供后续参考. ②前端方面:完成了登录界面的设计:各个界面的草图:为各个界面选取合适的图片如 ...