C++ without explicit loops:


  • 0
    M
    class Solution 
    {
    public:
        bool isAnagram(const string& s, const string& t) {
            int freq[256] = { 0 };
    
            for_each(
                begin(s), end(s),
                [&](char ch) { freq[ch]++; });
    
            for_each(
                begin(t), end(t),
                [&](char ch) { freq[ch]--; });
    
            auto okay = all_of(
                begin(freq), end(freq), 
                [](int cnt) { return cnt == 0; });
    
            return okay;
        }
    };
    

    Stylistically, it's probably better to use range-for instead of for_each, e.g.:

        for (auto ch : s) freq[ch]++;
        for (auto ch : t) freq[ch]--;
    

Log in to reply
 

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