Simple C++ Solution Counting Letters, O(n+m), 22ms


  • -1
    M
    class Solution {
    public:
        bool canConstruct(string ransomNote, string magazine) {
            vector<int> cnt(128);
            for (char c: magazine) ++cnt[c];
            for (char c: ransomNote) {
                if (cnt[c] <= 0) return false;
                --cnt[c];
            }
            return true;
        }
    };
    

Log in to reply
 

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