leetcode 383. Ransom Note
2016-08-20 12:30
351 查看
<span style="font-family: Arial, Helvetica, sans-serif;">public:</span>
bool canConstruct(string ransomNote, string magazine) { map<char,int> m; int len1=ransomNote.size(); int len2=magazine.size(); int icount=0; int fd=0; if(len1>len2){return false;} else { for (int i=0;i<len2;i++){ m[magazine[i]]++; } for (int j=0;j<len1;j++){ icount=m[ransomNote[j]]; if(icount!=0){ icount--; m[ransomNote[j]]=icount; fd++; } else { return false;} } if(fd==len1) {return true;} else return false; } } };
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.
canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true
相关文章推荐
- 小白笔记--------------leetcode(383. Ransom Note)
- LeetCode_383. Ransom Note
- leetcode 383. Ransom Note HashMap 统计字符 + HashMap
- 【leetcode】383. Ransom Note
- [LeetCode]383. Ransom Note
- [LeetCode] 383. Ransom Note
- Leetcode - String - 383. Ransom Note(水题)
- leetcode - 383. Ransom Note
- leetcode-383. Ransom Note
- leetcode 383. Ransom Note
- Leetcode 383. Ransom Note (Easy) (cpp)
- LeetCode383. Ransom Note优雅解法(线性时间复杂度)
- LeetCode 383. Ransom Note
- LeetCode-383. Ransom Note
- 【LeetCode】 383. Ransom Note
- [LeetCode]383. Ransom Note(赎金票据)
- leetcode 383. Ransom Note
- Leetcode 383. Ransom Note
- [LeetCode]--383. Ransom Note
- [leetcode] 383. Ransom Note 解题报告