[leetcode] 383. Ransom Note

网友投稿 240 2022-09-14

[leetcode] 383. Ransom Note

Description

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

Note: You may assume that both strings contain only lowercase letters.

canConstruct("a", "b") -> falsecanConstruct("aa", "ab") -> falsecanConstruct("aa", "aab") -> true

分析

题目的意思是: 给定连个random字符串和一个magazine字符串,问random可不可以由magazine字符串组成。

如果理解题意了,用一个map就可以搞定了,详情请看代码。

代码

class Solution {public: bool canConstruct(string ransomNote, string magazine) { unordered_map m; for(auto c:magazine){ m[c]++; } for(auto c:ransomNote){ if(--m[c]<0) return false; } return true; }};

参考文献

​​[LeetCode] Ransom Note 赎金条​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd7 in position 0: invalid continuation byte
下一篇:营销头版:利润暴跌90%,海底捞 “神话” 破灭?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~