C++ solution, using vector as hashmap, O(n)


  • 0
    A
    class Solution {
    public:
        bool canConstruct(string ransomNote, string magazine) {
            vector<int> counts(256, 0);
            for (char c : magazine)
                ++counts[c];
            
            for (char c : ransomNote) {
                if (!counts[c])
                    return false;
                --counts[c];
            }
            return true;
        }
    };
    

Log in to reply
 

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