forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrumbs22.cpp
More file actions
39 lines (31 loc) ยท 670 Bytes
/
crumbs22.cpp
File metadata and controls
39 lines (31 loc) ยท 670 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
#include <iostream>
#include <string>
using namespace std;
/*
TC:O(n)
SC:O(1)
ํ์ด๋ฐฉ๋ฒ:
- ascii ๊ธฐ์ค์ผ๋ก ๋ฌธ์ ๋น๋๋ฅผ ์ ์ฅํ cnt ๋ฐฐ์ด ์ ์ธ
- s์ ๊ฐ ๋ฌธ์ ๋ฑ์ฅ ํ์๋ฅผ cnt์ +1
- t์ ๊ฐ ๋ฌธ์๋ cnt์์ -1
- cnt ๋ฐฐ์ด์ ๋ชจ๋ ๊ฐ์ด 0์ด๋ฉด ๋ ๋ฌธ์์ด์ ์๋๊ทธ๋จ์ด๋ค
*/
class Solution {
public:
bool isAnagram(string s, string t) {
char cnt[256];
// cnt ๋ฐฐ์ด 0์ผ๋ก ์ด๊ธฐํ
for (char &value : cnt)
value = 0;
for (char ch : s)
cnt[ch]++;
for (char ch : t)
cnt[ch]--;
for (int i = 0; i < 256; i++)
{
if (cnt[i] != 0)
return (false);
}
return (true);
}
};