-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17-09-23-ValidAnagram.cpp
More file actions
49 lines (38 loc) · 1.17 KB
/
17-09-23-ValidAnagram.cpp
File metadata and controls
49 lines (38 loc) · 1.17 KB
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
45
46
47
48
49
/*
https://leetcode.com/problems/valid-anagram/
Time O(n), n == s.size();
Space O(n), n == s.size();
*/
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.size() != t.size()) return false;
unordered_map<char, int> hashAnagram;
for (int i = 0; i < s.size(); i++) { // O(n)
hashAnagram[s[i]]++;
}
for (int i = 0; i < t.size(); i++) { // O(n)
hashAnagram[t[i]]--;
if (hashAnagram[t[i]] < 0) {
return false;
}
}
return true;
}
};
/*
Approach 01 - Count all letters O(n²) | O(1)
Count how many 'a' string s has -> O(n)
Check if string t has the same amount of 'a' as string s -> O(n)
Count how many 'b' string s has -> O(n)
Check if string t has the same amount of 'b' as string s -> O(n)
...
Count how many 'z' string s has -> O(n)
Check if string t has the same amount of 'z' as string s -> O(n)
Considering only 26 possible characteres -> 26 * n * n -> O(n²)
Approach 02 - Sort
s.sort(); --> O(nlogn)
t.sort(); --> O(nlogn)
aaabbbccc aaabbbccc -> compair if s[i] == t[i] -> O(n)
Complexity: O(nlogn) + O(nlogn) + O(n) -> O(nlogn)
*/