Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.

Solution

bool isAnagram(string s, string t) {
        if(s.length() != t.length()) return false;
        //array<int, 26> number_cnt = {0};
        int number_cnt[26] = {0};
        for(auto const& c : s) {
            number_cnt[c-'a'] += 1;
        }
        for(auto const& c : t) {
            number_cnt[c-'a'] -= 1;
            if(number_cnt[c-'a'] < 0) {
                return false;
            }
        }
        return true;
    }

results matching ""

    No results matching ""