Clean 9 lines C++ use unordered_map


  • 0
    bool canConstruct(string ransomNote, string magazine) {
            unordered_map<char,int>m(26);
            int count=0;
            for(auto c: ransomNote) m[c]++;
            for(auto c:magazine){
                if(m[c]>0) count++;
                m[c]--;
                if(count==ransomNote.size()) return true;
            }
            return ransomNote.size()==0;
        }
    

Log in to reply
 

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