File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments