-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path118ValidAnagram.cpp
More file actions
44 lines (38 loc) · 872 Bytes
/
Copy path118ValidAnagram.cpp
File metadata and controls
44 lines (38 loc) · 872 Bytes
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;
#include<vector>
#include<unordered_map>
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size() != t.size()) return false;
unordered_map<char,int>count;
for(auto c : s){
count[c]++;
}
for(auto c : t){
count[c]--;
if(count[c] < 0) return false;
}
return true;
}
};
int main(){
Solution obj;
cout<<boolalpha;
cout<<obj.isAnagram("anagram","nagaram");
return 0;
}
/*
bool isAnagram(string s, string t) {
if(s.size() != t.size()) return false;
vector<int> freq(26, 0);
for(int i = 0; i < s.size(); i++){
freq[s[i] - 'a']++;
freq[t[i] - 'a']--;
}
for(int x : freq)
if(x != 0) return false;
return true;
}
*/