-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRansom Note.cpp
45 lines (34 loc) · 1.02 KB
/
Ransom Note.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
37
38
39
40
41
42
43
44
45
/* Leet Code */
/* Title - Ransom Note */
/* Created By - Akash Modak */
/* Date - 14/09/2021 */
// Given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise.
// Each letter in magazine can only be used once in ransomNote.
// Example 1:
// Input: ransomNote = "a", magazine = "b"
// Output: false
// Example 2:
// Input: ransomNote = "aa", magazine = "ab"
// Output: false
// Example 3:
// Input: ransomNote = "aa", magazine = "aab"
// Output: true
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char, int> m;
for(int i=0;i<magazine.length();i++){
m[magazine[i]]++;
}
for(int i=0;i<ransomNote.length();i++){
if(m.find(ransomNote[i]) == m.end()){
return false;
}
else if(m[ransomNote[i]] == 0){
return false;
}
m[ransomNote[i]]--;
}
return true;
}
};