File tree 1 file changed +12
-10
lines changed
1 file changed +12
-10
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
2
242. Valid Anagram
3
3
4
- First submitted: October 10, 2024
5
- This solution: November 21, 2024
4
+ Submitted: Decemebr 4, 2024
6
5
7
6
Runtime: 0 ms (beats 100.00%)
8
- Memory: 10.10 MB (beats 17.76 %)
7
+ Memory: 9.44 MB (beats 47.03 %)
9
8
*/
10
9
11
10
/*
12
11
Approach:
13
- We count the number of times each character appears in the two strings. If they
12
+ Count the number of times each character appears in the two strings. If they
14
13
are equal, they must be anagrams.
15
14
*/
16
15
17
16
class Solution {
18
17
public:
19
- bool isAnagram (string s, string t) {
20
- unordered_map< char , unsigned short > s_count ;
21
- unordered_map< char , unsigned short > t_count ;
18
+ bool isAnagram (const string& s, const string& t) {
19
+ uint16_t * s_count = ( uint16_t *) calloc ( 26 , sizeof ( uint16_t )) ;
20
+ uint16_t * t_count = ( uint16_t *) calloc ( 26 , sizeof ( uint16_t )) ;
22
21
for (const char & i : s) {
23
- s_count[i]++;
22
+ s_count[i - ' a ' ]++;
24
23
}
25
24
for (const char & i : t) {
26
- t_count[i]++;
25
+ t_count[i - ' a ' ]++;
27
26
}
28
- return s_count == t_count;
27
+ for (uint8_t i = 0 ; i < 26 ; ++i) {
28
+ if (s_count[i] != t_count[i]) return false ;
29
+ }
30
+ return true ;
29
31
}
30
32
};
You can’t perform that action at this time.
0 commit comments