O(N) C++ solution


  • 0
    K

    Where N = len( Note ) + len( Mag ).

    class Solution {
    public:
        bool canConstruct(string ransomNote, string magazine) {
            unordered_map<char, int> counts;
            
            for(char ch: ransomNote) ++counts[ch];
            
            for(char ch: magazine){
                if(counts.find(ch) != counts.end()) --counts[ch];
            }
            
            bool canMake = true;
            for(auto kv: counts){
                canMake &= kv.second<=0;
            }
            
            return canMake;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.