Skip to content

Commit cf8fd9c

Browse files
committed
updated 242
1 parent 8a73977 commit cf8fd9c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

0242-valid-anagram.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
/*
22
242. Valid Anagram
33
4-
First submitted: October 10, 2024
5-
This solution: November 21, 2024
4+
Submitted: Decemebr 4, 2024
65
76
Runtime: 0 ms (beats 100.00%)
8-
Memory: 10.10 MB (beats 17.76%)
7+
Memory: 9.44 MB (beats 47.03%)
98
*/
109

1110
/*
1211
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
1413
are equal, they must be anagrams.
1514
*/
1615

1716
class Solution {
1817
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));
2221
for (const char& i : s) {
23-
s_count[i]++;
22+
s_count[i - 'a']++;
2423
}
2524
for (const char& i : t) {
26-
t_count[i]++;
25+
t_count[i - 'a']++;
2726
}
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;
2931
}
3032
};

0 commit comments

Comments
 (0)