-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path242.cpp
More file actions
executable file
·42 lines (34 loc) · 799 Bytes
/
Copy path242.cpp
File metadata and controls
executable file
·42 lines (34 loc) · 799 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
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.length() != t.length())
return false;
unordered_map<char, int>countS;
unordered_map<char, int>countT;
for(int i=0; i < s.length(); i++){
countS[s[i]]++;
countT[t[i]]++;
}
return countS == countT;
}
};
// TC - O(n+m)
// SC - O(1)
/* bool isAnagram(string s, string t) {
if (s.length() != t.length()) return false;
vector<int> count(26, 0);
for (char c : s) {
count[c - 'a']++;
}
for (char c : t) {
count[c - 'a']--;
}
for (int val : count) {
if (val != 0) return false;
}
return true;
} */