File tree Expand file tree Collapse file tree 5 files changed +70
-0
lines changed
product-of-array-except-self
validate-binary-search-tree Expand file tree Collapse file tree 5 files changed +70
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ ``` cpp
3+ class Solution {
4+ public:
5+ bool isAnagram(string s, string t) {
6+ if (s.length() != t.length())
7+ return false;
8+ sort(s.begin(), s.end());
9+ sort(t.begin(), t.end());
10+ return s==t;
11+ }
12+ };
13+ ```
14+
15+ - O(nlogn)
16+ - std์ sort๋ฅผ ์ฌ์ฉ
17+
18+ ```
19+ class Solution {
20+ public:
21+ bool isAnagram(string s, string t) {
22+ if (s.length() != t.length())
23+ return false;
24+ unordered_map<char, int> dict;
25+ for(auto c: s ){
26+ dict[ c] ++;
27+ }
28+ for(auto c: t ){
29+ if(dict[ c] ==0){
30+ return false;
31+ }
32+ dict[ c] --;
33+ }
34+ return true;
35+
36+ }
37+ };
38+ ```
39+
40+ - O(n)? => O(nlogn)
41+ - map insert์์ O(logn)์ฉ ์์๋จ
42+ - ์์ sort๋ณด๋ค๋ ํจ์จ์ ์ผ ์ ์์
43+
44+ ```cpp
45+ class Solution {
46+ public:
47+ bool isAnagram(string s, string t) {
48+ if (s.length() != t.length())
49+ return false;
50+ vector<int>cntVec(26, 0);
51+ for(auto c:s){
52+ cntVec[(int(c-'a'))]++;
53+ }
54+ for(auto c:t){
55+ cntVec[(int(c-'a'))]--;
56+ }
57+ for(auto cnt:cntVec){
58+ if(cnt!=0){
59+ return false;
60+ }
61+ }
62+ return true;
63+
64+ }
65+ };
66+ ```
67+ - O(n)
68+ - ๋๋ฒ์งธ ๊ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ ์ฅ๊ณต๊ฐ์ด ํ์ํ๋, O(n)์ผ๋ก ์ข
๋ฃ ๊ฐ๋ฅ
69+ - ์๊ฐ ๋ณต์ก๋๋ฅผ ์ต๋ํ ์ต์ ํ ํ๋ ๊ฒฝ์ฐ
70+
You canโt perform that action at this time.
0 commit comments