-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathValid_Anagram.cpp
More file actions
32 lines (26 loc) · 783 Bytes
/
Valid_Anagram.cpp
File metadata and controls
32 lines (26 loc) · 783 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
class Solution {
public:
void countingSort(string& str) {
size_t len = str.length();
char output[len];
const int RANGE = 26;
int count[RANGE + 1];
memset(count, 0, sizeof(count));
for(int i = 0; i < len; ++i) ++count[str[i] - 'a'];
for (int i = 1; i <= RANGE; ++i) count[i] += count[i - 1];
for (int i = 0; i < len; ++i) {
output[count[str[i] - 'a'] - 1] = str[i];
--count[str[i] - 'a'];
}
for (int i = 0; i < len; ++i) str[i] = output[i];
}
bool isAnagram(string s, string t) {
/*
sort(s.begin(), s.end());
sort(t.begin(), t.end());
*/
countingSort(s);
countingSort(t);
return s == t;
}
};