Leetcode-916. Word Subsets-(Medium)
一、问题描述
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,如果B中的每个元素串中的每个字符都在一个字符串中(计算重复);那么说明这个字符串符合条件
输入两个字符串数组,A、B;B为模式字符串数组;判断A中的多少个字符串符合预期?
二、解答
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #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 参数
分类:
算法相关
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2017-12-28 CAShapeLayer