一、问题描述

  

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. node统计指定文件夹内代码行数

    1. 来源 想对于自己接触前端日常学习与思考的代码行数进行一个统计,看自己大约敲了多少代码 2.代码 const fs = require('fs') const path = require('pa ...

  2. dva使用yarn编译出错

    1. 报错信息 ./src/models/example.jsModule build failed: TypeError: /Users/user/Desktop/learn-code/10.Rea ...

  3. 【Oracle】使用like的时候遇到的问题

    [Oracle]使用like的时候遇到的问题 like语句其中的%就代表着一个零或者多个字符,_代表一个字符,%与_可以同时使用 name想查询以'_'结尾的字符 用这个语句就会有问题 select ...

  4. SQL case when then end

    SQL case when then end sql中的返回值可以使用case when then end来进行实现 SELECT s.s_id, CASE s.s_name WHEN '1' THE ...

  5. 力扣1107(MySQL)-每日新用户统计(中等)

    题目: Traffic 表: 该表没有主键,它可能有重复的行.activity 列是 ENUM 类型,可能取 ('login', 'logout', 'jobs', 'groups', 'homepa ...

  6. 力扣176(MySQL)-第二高的薪水(中等)

    题目: id 是这个表的主键.表的每一行包含员工的工资信息. 编写一个 SQL 查询,获取并返回 Employee 表中第二高的薪水 .如果不存在第二高的薪水,查询应该返回 null . 查询结果如下 ...

  7. 牛客网-SQL专项训练23

    ①假设创建新用户nkw,现在想对于任何IP的连接,仅拥有user数据库里面的select和insert权限,则列表语句中能够实现这一要求的语句是(B) 解析: 考察知识点-数据库授权命令: GRANT ...

  8. 400倍加速, PolarDB HTAP实时数据分析技术解密

    简介: PolarDB MySQL是因云而生的一个数据库系统, 除了云上OLTP场景,大量客户也对PolarDB提出了实时数据分析的性能需求.对此PolarDB技术团队提出了In-Memory Col ...

  9. 基于DataWorks搭建新零售数据中台

    文章作者:许日(欢伯),在2016年盒马早期的时候,转到盒马事业部作为在线数据平台的研发负责人,现任阿里云计算平台DataWorks建模引擎团队负责人. 文章简介:本篇文章向大家分享新零售企业如何基于 ...

  10. [ML] Tensorflow2 保存完整模型以及使用 HDF5

    将模型保存为完整的 HDF5 文件,后面可以直接加载使用: # cnblogs.com/farwish import tenforflow as tf model = tf.keras.models. ...