一、问题描述

  

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. Weblogic、Tomcat、Apache、Nginx等web容器学习笔记

    1.weblogic weblogic是美国Oracle公司的一款产品,是一个基于JAVAEE架构的中间件.是用于开发.集成.部署 .管理大型分布式Web应用.网络应用.数据库应用的Java应用服务器 ...

  2. ecplice 如何智能提示(旧)

    前言 从my老博客迁移过来的. 正文 1.java智能提示 (1). 打开Eclipse,选择打开" Window - Preferences". (2). 在目录树上选择&quo ...

  3. mmdeploy源码安装 (转换faster rcnn r50/yolox为tensorrt,并用mmdeploy sdk推理)

    mmdeploy源码安装 (转换faster rcnn r50/yolox为tensorrt,并进行推理) 这个系列是一个随笔,是我走过的一些路,有些地方可能不太完善.如果有那个地方没看懂,评论区问就 ...

  4. Cache Aside Pattern缓存+数据库读写模式的分析

    1.Cache Aside Pattern(1)读的时候,先读缓存,缓存没有的话,那么就读数据库,然后取出数据后放入缓存,同时返回响应 (2)更新的时候,先删除缓存,然后再更新数据库 2.为什么是删除 ...

  5. Worker 进行多线程任务开发

    概念介绍 在 OpenHarmony 中,UI 线程负责处理 UI 事件和用户交互,而 Worker 线程用于处理耗时操作,以提高应用程序的响应速度和用户体验. Worker 线程是与主线程并行的独立 ...

  6. vue登录3D效果

    实现的效果 登录动态效果很炫酷,话不多说直接上代码: 组件template <template> <div class="entrance"> <di ...

  7. 记录如何用php做一个网站访问计数器的方法

    简介创建一个简单的网站访问计数器涉及到几个步骤,包括创建一个用于存储访问次数的文件或数据库表,以及编写PHP脚本来增加计数和显示当前的访问次数. 方法以下是使用文件存储访问次数的基本步骤: 创建一个文 ...

  8. 合阔智云核心生产系统切换到服务网格 ASM 的落地实践

    简介: 合阔智云提供了从全渠道交易管理到订单履约再到门店供应链完整的餐饮零售连锁解决方案,整个方案采取微服务设计,并深度使用了 Kubernetes 作为生产调度平台. 作者:刘如鸿   背景 合阔智 ...

  9. 【详谈 Delta Lake 】系列技术专题 之 特性(Features)

    简介: 本文翻译自大数据技术公司 Databricks 针对数据湖 Delta Lake 的系列技术文章.众所周知,Databricks 主导着开源大数据社区 Apache Spark.Delta L ...

  10. WPF 已知问题 RadioButton 指定 GroupName 后关闭窗口可能导致无法选中

    本文记录一个 WPF 已知问题,当 WPF 的 RadioButton 指定 GroupName 且将 IsChecked 状态绑定到 ViewModel 上,将包含以上控件的代码的窗口显示两个,接着 ...