Ransom Note


https://leetcode.com/problems/ransom-note/

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        int count[26]={0};
        for(char c : magazine){
            count[c-'a']++;
        }
        for(char ch : ransomNote){
            if(count[ch-'a']-- <=0){
                return false;
            }
        }
        return true;
    }
}; 


class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map<char , int >m;
        int count=0;
        for(int i=0;i<magazine.length();i++){
            m[magazine[i]]++;
        }
        for(int i=0;i<ransomNote.length();i++){
           if(m[ransomNote[i]]==0){
               return false;
           }
           m[ransomNote[i]]--;
        }
        return true;
    }
};

Comments

Popular posts from this blog

Perfect Peak of Array

Is Rectangle?

Sort array with squares!