Skip to content

Commit 6906cda

Browse files
committed
New Problem Solution - "Ransom Note"
1 parent d666cbf commit 6906cda

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LeetCode
1313
|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) | [C++](./algorithms/cpp/lexicographicalNumbers/LexicographicalNumbers.cpp)|Medium|
1414
|385|[Mini Parser](https://leetcode.com/problems/mini-parser/) | [C++](./algorithms/cpp/miniParser/MiniParser.cpp)|Medium|
1515
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [C++](./algorithms/cpp/shuffleAnArray/ShuffleAnArray.cpp)|Medium|
16+
|383|[Ransom Note](https://leetcode.com/problems/ransom-note/) | [C++](./algorithms/cpp/ransomNote/RansomNote.cpp)|Easy|
1617
|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | [C++](./algorithms/cpp/combinationSumIV/combinationSumIV.cpp)|Medium|
1718
|376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | [C++](./algorithms/cpp/wiggleSubsequence/wiggleSubsequence.cpp)|Medium|
1819
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [C++](./algorithms/cpp/intersectionOfTwoArraysII/intersectionOfTwoArraysII.cpp)|Easy|
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Source : https://leetcode.com/problems/ransom-note/
2+
// Author : Hao Chen
3+
// Date : 2016-08-24
4+
5+
/***************************************************************************************
6+
*
7+
* 
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing
8+
* 
letters from
 all 
the 
magazines,
 write 
a 
function 
that 
will 
return 
true
9+
* 
if 
the 
ransom 

10+
* note 
can 
be 
constructed 
from 
the 
magazines ; 
otherwise, 
it 
will 
return
11+
* 
false. 


12+
*
13+
* Each 
letter
 in
 the
 magazine 
string 
can
 only 
be
 used 
once
 in
 your
14+
* 
ransom
 note.
15+
*
16+
* Note:
17+
* You may assume that both strings contain only lowercase letters.
18+
*
19+
* canConstruct("a", "b") -> false
20+
* canConstruct("aa", "ab") -> false
21+
* canConstruct("aa", "aab") -> true
22+
***************************************************************************************/
23+
24+
class Solution {
25+
public:
26+
bool canConstruct(string ransomNote, string magazine) {
27+
unordered_map<char, int> m;
28+
for(int i=0; i<magazine.size(); i++) {
29+
m[magazine[i]]++;
30+
}
31+
for (int i=0; i<ransomNote.size(); i++) {
32+
char c = ransomNote[i];
33+
if (m[c] <=0 ) return false;
34+
m[c]--;
35+
}
36+
return true;
37+
}
38+
};

0 commit comments

Comments
 (0)