From d61801b8ba438bf39fdbebffde9da32b904c1efa Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Wed, 24 Aug 2016 13:24:30 +0800 Subject: [PATCH] New Problem Solution - "Ransom Note" --- README.md | 1 + algorithms/cpp/ransomNote/RansomNote.cpp | 38 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 algorithms/cpp/ransomNote/RansomNote.cpp diff --git a/README.md b/README.md index 477d66c..d9e0f38 100644 --- a/README.md +++ b/README.md @@ -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| diff --git a/algorithms/cpp/ransomNote/RansomNote.cpp b/algorithms/cpp/ransomNote/RansomNote.cpp new file mode 100644 index 0000000..c13922e --- /dev/null +++ b/algorithms/cpp/ransomNote/RansomNote.cpp @@ -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 m; + for(int i=0; i