File tree 2 files changed +39
-0
lines changed
algorithms/cpp/ransomNote
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ LeetCode
13
13
| 386| [ Lexicographical Numbers] ( https://leetcode.com/problems/lexicographical-numbers/ ) | [ C++] ( ./algorithms/cpp/lexicographicalNumbers/LexicographicalNumbers.cpp ) | Medium|
14
14
| 385| [ Mini Parser] ( https://leetcode.com/problems/mini-parser/ ) | [ C++] ( ./algorithms/cpp/miniParser/MiniParser.cpp ) | Medium|
15
15
| 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|
16
17
| 377| [ Combination Sum IV] ( https://leetcode.com/problems/combination-sum-iv/ ) | [ C++] ( ./algorithms/cpp/combinationSumIV/combinationSumIV.cpp ) | Medium|
17
18
| 376| [ Wiggle Subsequence] ( https://leetcode.com/problems/wiggle-subsequence/ ) | [ C++] ( ./algorithms/cpp/wiggleSubsequence/wiggleSubsequence.cpp ) | Medium|
18
19
| 350| [ Intersection of Two Arrays II] ( https://leetcode.com/problems/intersection-of-two-arrays-ii/ ) | [ C++] ( ./algorithms/cpp/intersectionOfTwoArraysII/intersectionOfTwoArraysII.cpp ) | Easy|
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments