码迷,mamicode.com
首页 > 其他好文 > 详细

383. Ransom Note

时间:2017-03-25 19:46:01      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:++   const   cte   字母   c++   not   pre   class   logs   

 

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") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true


第二个字符串包含第一个字符串的所有字母,就返回true,不一定要连续包含
只要第一个字符串中某个字母的数量 大于 第二个字符串的这个字母的数量 则返回false

C++(19ms):

 1 class Solution {
 2 public:
 3     bool canConstruct(string ransomNote, string magazine) {
 4         vector<int> magazLetters(128, 0);
 5         for(char m : magazine)
 6             magazLetters[m]++;
 7         for(char r : ransomNote)
 8             if(--magazLetters[r]<0) return false;
 9         return true;
10     }
11 };

 

383. Ransom Note

标签:++   const   cte   字母   c++   not   pre   class   logs   

原文地址:http://www.cnblogs.com/-Buff-/p/6618138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!