File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ // ์๋ฐ์์๋ ์๊ฐ๋ณต์ก๋๋ฅผ O(N)์ผ๋ก ์ก์๋ ์ต์์๊ถ์ผ๋ก ๊ฐ ์ ์๋ ๋ฌธ์
2
+ // ์ ๋์ฝ๋ ๊ณ ๋ ค
3
+ public boolean isAnagram (String s , String t ) {
4
+ if (s .length () != t .length ()) return false ;
5
+
6
+ Map <Character , Integer > map = new HashMap <>();
7
+ for (int i = 0 ; i < s .length (); i ++) {
8
+ map .put (s .charAt (i ), map .getOrDefault (s .charAt (i ), 0 ) + 1 );
9
+ map .put (t .charAt (i ), map .getOrDefault (t .charAt (i ), 0 ) - 1 );
10
+ }
11
+ for (int value : map .values ()) {
12
+ if (value != 0 ) return false ;
13
+ }
14
+ return true ;
15
+ }
16
+ // ์ํ๋ฒณ๋ง ๊ณ ๋ ค
17
+ public boolean isAnagram (String s , String t ) {
18
+ int ALPHABET_COUNT = 26 ;
19
+ if (s .length () != t .length ()) {
20
+ return false ;
21
+ }
22
+ int [] arr = new int [ALPHABET_COUNT ]; // ์ํ๋ฒณ ๊ฐฏ์
23
+ for (int i = 0 ; i < s .length () ; i ++) {
24
+ arr [s .charAt (i ) - 97 ]++;
25
+ arr [t .charAt (i ) - 97 ]--;
26
+ }
27
+ for (int i = 0 ; i < ALPHABET_COUNT ; i ++) {
28
+ if (arr [i ] != 0 ) {
29
+ return false ;
30
+ }
31
+ }
32
+ return true ;
33
+ }
You canโt perform that action at this time.
0 commit comments