Skip to content

Commit 8a9ec3a

Browse files
committed
group anagrams
1 parent db7ac9c commit 8a9ec3a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

group-anagrams/se6816.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
Map을 통해 사전순으로 정렬된 문자열의 집합을 구한 뒤 그 집합들을 리턴하는 방식
3+
strs의 길이 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(N)
6+
*/
7+
class Solution {
8+
public List<List<String>> groupAnagrams(String[] strs) {
9+
List<List<String>> result = new ArrayList<>();
10+
Map<String, List<String>> hashMap = new HashMap<>();
11+
for(int i=0; i<strs.length;i++){
12+
char[] charArray = strs[i].toCharArray();
13+
Arrays.sort(charArray);
14+
String word = new String(charArray);
15+
List<String> list = hashMap.getOrDefault(word, new ArrayList<>());
16+
list.add(strs[i]);
17+
hashMap.put(word,list);
18+
}
19+
for(Map.Entry<String, List<String>> entry : hashMap.entrySet()){
20+
List<String> list = entry.getValue();
21+
result.add(list);
22+
}
23+
return result;
24+
}
25+
}
26+
27+
28+

0 commit comments

Comments
 (0)