C++ vector solution

  • 1

    First store the count of characters in magazine, then scan through ransomNote.

         bool canConstruct(string ransomNote, string magazine) {
            vector<int> m(128, 0);
            for (char d : magazine) m[d]++;
            for (char c: ransomNote){
                if (m[c]==0) return false;
            return true;

Log in to reply

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