New Problem Solution - "Ransom Note"
This commit is contained in:
parent
04f616ece5
commit
d61801b8ba
@ -13,6 +13,7 @@ LeetCode
|
||||
|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) | [C++](./algorithms/cpp/lexicographicalNumbers/LexicographicalNumbers.cpp)|Medium|
|
||||
|385|[Mini Parser](https://leetcode.com/problems/mini-parser/) | [C++](./algorithms/cpp/miniParser/MiniParser.cpp)|Medium|
|
||||
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [C++](./algorithms/cpp/shuffleAnArray/ShuffleAnArray.cpp)|Medium|
|
||||
|383|[Ransom Note](https://leetcode.com/problems/ransom-note/) | [C++](./algorithms/cpp/ransomNote/RansomNote.cpp)|Easy|
|
||||
|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | [C++](./algorithms/cpp/combinationSumIV/combinationSumIV.cpp)|Medium|
|
||||
|376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | [C++](./algorithms/cpp/wiggleSubsequence/wiggleSubsequence.cpp)|Medium|
|
||||
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [C++](./algorithms/cpp/intersectionOfTwoArraysII/intersectionOfTwoArraysII.cpp)|Easy|
|
||||
|
38
algorithms/cpp/ransomNote/RansomNote.cpp
Normal file
38
algorithms/cpp/ransomNote/RansomNote.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
// Source : https://leetcode.com/problems/ransom-note/
|
||||
// Author : Hao Chen
|
||||
// Date : 2016-08-24
|
||||
|
||||
/***************************************************************************************
|
||||
*
|
||||
*
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.
|
||||
*
|
||||
* Note:
|
||||
* You may assume that both strings contain only lowercase letters.
|
||||
*
|
||||
* canConstruct("a", "b") -> false
|
||||
* canConstruct("aa", "ab") -> false
|
||||
* canConstruct("aa", "aab") -> true
|
||||
***************************************************************************************/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool canConstruct(string ransomNote, string magazine) {
|
||||
unordered_map<char, int> m;
|
||||
for(int i=0; i<magazine.size(); i++) {
|
||||
m[magazine[i]]++;
|
||||
}
|
||||
for (int i=0; i<ransomNote.size(); i++) {
|
||||
char c = ransomNote[i];
|
||||
if (m[c] <=0 ) return false;
|
||||
m[c]--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user