# Leetcode [No.383] Ransom Note (EASY) ## 題目 https://leetcode.com/problems/ransom-note/ ## 思路 + 這個題目算滿常見的,反正就是用26個字母來記錄,如果哪天小於0了就return false; ```c++ class Solution { public: bool canConstruct(string ransomNote, string magazine) { int alphabet_array[26] = {}; for(int i = 0 ; i < magazine.size(); i++) { alphabet_array[magazine[i]-'a'] ++; } for(int i = 0 ; i < ransomNote.size(); i++) { alphabet_array[ransomNote[i]-'a']--; if(alphabet_array[ransomNote[i]-'a'] < 0) { return false; } } return true; } }; ``` ### 解法分析 + time complexity: O(nlgn) ### 執行結果 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up