summaryrefslogtreecommitdiff
path: root/top-interview-questions/easy/strings/04_valid_anagram.cc
blob: 3ca6b7c572f5279c93ce1f3b291d3732489dccd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
    static void count(const string& s, int* counts) {
        for (char c : s) {
            counts[c]++;
        }
    }

    bool isAnagram(string s, string t) {
        if (s.size() != t.size()) {
            return false;
        }
        // strings are equal length

        int sCount[256] = {};
        int tCount[256] = {};

        count(s, sCount);
        count(t, tCount);

        for (char c : s) {
            if (sCount[c] != tCount[c]) {
                return false;
            }
        }

        return true;
    }
};