File tree Expand file tree Collapse file tree 1 file changed +33
-1
lines changed Expand file tree Collapse file tree 1 file changed +33
-1
lines changed Original file line number Diff line number Diff line change
1
+ // Map을 사용한 버전
2
+ class Solution {
3
+ public boolean isAnagram (String s , String t ) {
4
+ if (s .length () != t .length ()) return false ;
5
+
6
+ Map <Character , Integer > alphabet = new HashMap <>();
7
+
8
+ for (char c : s .toCharArray ()){
9
+ if (!alphabet .containsKey (c )){
10
+ alphabet .put (c , 0 );
11
+ }
12
+ alphabet .put (c , alphabet .get (c ) + 1 );
13
+ }
14
+
15
+ for (char c : t .toCharArray ()){
16
+ if (!alphabet .containsKey (c )) return false ;
17
+ if (alphabet .get (c ) == 0 ) return false ;
18
+
19
+ alphabet .put (c , alphabet .get (c )-1 );
20
+ }
21
+
22
+ return true ;
23
+ }
24
+ }
25
+
26
+
27
+ // 초기 버전
1
28
class Solution {
2
29
public boolean isAnagram (String s , String t ) {
3
30
int [] character = new int [26 ];
@@ -17,4 +44,9 @@ public boolean isAnagram(String s, String t) {
17
44
}
18
45
}
19
46
20
-
47
+ /*
48
+ Map, 배열 모두 평균시간복잡도는 O(1)이지만,
49
+ 배열이 직접 접근 방식이고, Map은 Hash를 사용하여서 배열 보다는 시간이 더 걸린다
50
+ 배열 사용시 4ms
51
+ Map 사용 시 17ms
52
+ */
You can’t perform that action at this time.
0 commit comments