C++ clear solution with array


  • 1
    class Solution {
    public:
        bool canConstruct(string ransomNote, string magazine) {
            int m[26]={0};
            for(auto item: magazine) m[item-'a']++;
            for(auto item: ransomNote)
            {
                    if(--m[item-'a'] <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.