easy O(n) c++ solution using hash table


  • 0
    bool canConstruct(string ransomNote, string magazine) 
    {
        int hash[26] = {0};
        
        for (auto c : magazine)
            hash[c - 'a'] ++;
            
        for (auto c : ransomNote)
            if (hash[c - 'a'] > 0)
                hash[c - 'a'] --;
            else
                return false;
    
        return true;
    }

Log in to reply
 

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