Skip to content

Commit 20dc9f6

Browse files
committed
solve valid anangram problem
1 parent dcd9377 commit 20dc9f6

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

valid-anagram/sora0319.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
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+
// 초기 버전
128
class Solution {
229
public boolean isAnagram(String s, String t) {
330
int[] character = new int[26];
@@ -17,4 +44,9 @@ public boolean isAnagram(String s, String t) {
1744
}
1845
}
1946

20-
47+
/*
48+
Map, 배열 모두 평균시간복잡도는 O(1)이지만,
49+
배열이 직접 접근 방식이고, Map은 Hash를 사용하여서 배열 보다는 시간이 더 걸린다
50+
배열 사용시 4ms
51+
Map 사용 시 17ms
52+
*/

0 commit comments

Comments
 (0)