aboutsummaryrefslogtreecommitdiff
path: root/valid_anagram.cpp
blob: 2c2586e5e289461db6059189d8bc1ae06815fdec (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
#include "leetcode.h"

class Solution {
public:
  bool isAnagram(string s, string t) {
    if (s == t)
      return true;
    if (s.size() != t.size())
      return false;
    unordered_map<char, int> up;
    for (int i = 0; i < s.size(); i++) {
      up[s[i]]++;
      up[t[i]]--;
    }
    for (auto it : up)
      if (it.second)
        return false;
    return true;
  }
};

int main() {
  Solution obj;
  string s = "anagram", t = "nagaram";
  if (obj.isAnagram(s, t))
    cout << "true";
  else
    cout << "false";
}