-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMay_RansomNote.cpp
36 lines (33 loc) · 1.01 KB
/
May_RansomNote.cpp
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
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
map<char,int> inputMap;
map<char, int> targetMap;
for(int i = 0; i < ransomNote.length(); i++){
if(inputMap.count(ransomNote[i]) > 0){
inputMap[ransomNote[i]]++;
}
else{
inputMap[ransomNote[i]] = 1;
}
}
for(int i = 0; i < magazine.length(); i++){
if(targetMap.count(magazine[i]) > 0){
targetMap[magazine[i]]++;
}
else{
targetMap[magazine[i]] = 1;
}
}
map<char,int>::iterator iter;
bool result = true;
for(iter = inputMap.begin(); iter != inputMap.end(); iter++){
if(targetMap.count(iter->first) > 0 && targetMap[iter->first] >= iter->second) continue;
else{
result = false;
break;
}
}
return result;
}
};