一、问题描述

  

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].
 
  解释:
  给定字符串数组B,如果B中的每个元素串中的每个字符都在一个字符串中(计算重复);那么说明这个字符串符合条件
  输入两个字符串数组,A、B;B为模式字符串数组;判断A中的多少个字符串符合预期?
 
二、解答
  

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
using namespace std; bool isUniversal(string& word, std::map<char, int> chr_map)
{
for(auto c:word){
if(chr_map.find(c) != chr_map.end())
chr_map[c] -= 1;
} std::map<char, int>::iterator it = chr_map.begin();
while (it != chr_map.end()) {
if((it->second) > 0)
return false;
it++;
} return true;
} class Solution {
public:
vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
std::map<char, int> chr_map;
std::vector<string> result;
for(auto word:B){
std::map<char, int> chr_temp_map;
for(auto c:word){
chr_temp_map[c] += 1;
}
for (auto& kv : chr_temp_map) {
chr_map[kv.first] = std::max(kv.second, chr_map[kv.first]);
}
} for(auto w: A){
if(isUniversal(w, chr_map))
{
result.push_back(w);
}
}
return result;
}
}; void test()
{
std::map<char, int> chr_map;
std::vector<string> result{"abc","bcd","cde"};
for(auto word:result){
for(auto c:word){
chr_map[c] += 1;
cout<<chr_map[c]<<endl;
}
} std::map<char, int> chr_map_copy(chr_map); } int main(int argc, const char * argv[]) {
// insert code here...
// test();
Solution s;
vector<string> A{"amazon","apple","facebook","google","leetcode"};
vector<string> B{"lo","eo"};
s.wordSubsets(A, B); return 0;
}

  

三、总结

  这道题目没有特殊,主要是理解题意。

  用到std::<string> map,for 遍历 c++11的语法

  编译需要添加 -std=c++11 参数

p.p1 { margin: 0; font: 29px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }

Leetcode-916. Word Subsets-(Medium)的更多相关文章

  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. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  5. 916. Word Subsets

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

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  8. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  9. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  10. 【一天一道LeetCode】#90. Subsets II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. 重新整理.net core 计1400篇[七] (.net core 中的依赖注入)

    前言 请阅读第六篇,对于理解.net core 中的依赖注入很关键. 和我们上一篇不同的是,.net core服务注入保存在IServiceCollection 中,而将集合创建的依赖注入容器体现为I ...

  2. 6980. 【2021.02.03冬令营模拟】你的世界(world) Another Solution

    Problem Description Input 从文件 world.in 中读入数据. Output 输出到文件 world.out 中. 输出共 T 行,第 i 行表示第 i 组测试数据的答案, ...

  3. 第一次blog

    前言:我在大一上学期学习了c语言,然后在下学期学习了第二门语言java,因为之前c语言学的挺一般的,然后在这学期学习java感觉还是挺不简单的,要自学很多东西,在这段时间里,我学习了JAVA的基本语法 ...

  4. 持续定义Saas模式云数据仓库+实时分析

    简介: 从实时分析的价值.场景和数据流程,以及用户对平台能力要求展开,讲述云数据仓库MaxCompute的产品能力优势 ,面对实时分析场景的能力演进要求.进而以实时分析典型场景的全数据流程处理.建模和 ...

  5. [GPT] 怎么查看我的 macbook 有多少显存

      您可以按照以下步骤查看您MacBook的显存大小: 点击屏幕左上角的苹果图标,选择"关于本机". 在弹出的窗口中,点击"系统报告". 在左侧栏中选择&quo ...

  6. [FE] yarn, npm 切换镜像源

    yarn 设置命令如下,会修改 ~/.yarnrc 内容. $ yarn config set registry https://registry.yarnpkg.com npm 设置命令如下,会修改 ...

  7. [Py] Jupyter 写入和执行 python 文件

    以 %%writefile request.py 开头. 下面写 python 代码,然后 shift + enter 键,可以把 python 代码写入开头指定的文件中,没有则自动创建. 以 %ru ...

  8. dotnet 7 已知问题 WPF 的 TreeView 开启虚拟化之后只显示首项

    本文记录 WPF 在 dotnet 7 的一个已知问题,此问题当前已修复,只需更新 SDK 或运行时即可.使用 TreeView 在开启虚拟化之后只显示首项,其他项不显示.本文将告诉大家此问题的原因和 ...

  9. QT之Mysql驱动

    错误现象 找不到Mysql驱动 QSqlDatabase: QMYSQL driver not loaded 一.驱动查看 在程序中直接打印QT Creator中现有的驱动,打印方式如下: qDebu ...

  10. mosquitto的安装与使用

      一款实现了消息推送协议 MQTT v3.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功耗传感器,手机.嵌入式 ...