Hashmap based c++ solution


  • 0
    S

    class Solution {
    public:
    bool canConstruct(string r, string m) {

        int i=0,j=0;
        map <char ,int> h;
        while(i<m.length())
        {    h[m[i]]++;
            i++;
        }
        i=0;
        
        while(i<r.length())
        {
            if(h.find(r[i])!=h.end())
            {
                h[r[i]]=h[r[i]]-1;
                if(h[r[i]]==0)
                h.erase(h.find(r[i]));
            }
            else
                return false;
                i++;
        }
        return true;
        
    }
    

    };


Log in to reply
 

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