LC 383. Ransom Note

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        vector<int> freq_note(26,0);
        vector<int> freq_mags(26,0);
        for(int i=0;i<26;i++){
            freq_note[i] = count(ransomNote.begin(),ransomNote.end(),'a'+i);
            freq_mags[i] = count(magazine.begin(),magazine.end(),'a'+i);
            if(freq_note[i]<=freq_mags[i])
                continue;
            else
                return false;
        }
        return true;
    }
};


Dicussion Forum