File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Solution {
2+ public List <List <String >> groupAnagrams (String [] strs ) {
3+ /**
4+ 1.anagram ๋ผ๋ฆฌ ๊ทธ๋ฃนํํด์ return
5+ 2.์กฐ๊ฑด
6+ - answer in any order
7+ - strs ๊ธธ์ด ์ต์ = 1, ์ต๋ = 10^4
8+ - ์์ ํ๋๋น ๊ธธ์ด ์ต์ = 0, ์ต๋ = 100
9+ - ๋ชจ๋ ์๋ฌธ์๋ก ๊ตฌ์ฑ
10+ 3.ํ์ด
11+ - 1) i๋ฒ์งธ ์ดํ ๋จ์ด๋ฅผ ๋น๊ตํด๊ฐ๋ฉด์ anagram check, time: O(n^2)
12+ - 2) HashMap: string element ๋ฅผ ์ ๋ ฌํด์ key ๋ก ์ฌ์ฉ, ์ค๋ณต๋๋ Key ์์ผ๋ฉด anagram, time: O(n)
13+ */
14+
15+ int n = strs .length ;
16+ //anagram ์ฒดํฌํ Map
17+ Map <String , List <String >> map = new HashMap <>();
18+
19+ for (int i = 0 ; i < n ; i ++) {
20+ String curStr = strs [i ];
21+ char [] tmp = curStr .toCharArray ();
22+ Arrays .sort (tmp );
23+ String key = new String (tmp );
24+ // System.out.println("curStr:" + curStr + ", key: " + key);
25+ //Map ์ ์ ์ฅ
26+ map .putIfAbsent (key , new ArrayList <>());
27+ map .get (key ).add (curStr );
28+ // if(!map.containsKey(key)) {
29+ // List<String> words = new ArrayList<>();
30+ // words.add(curStr);
31+ // map.put(key, words);
32+ // } else {
33+ // List<String> words = map.get(key);
34+ // words.add(curStr);
35+ // map.put(key, words);
36+ // }
37+ }
38+
39+
40+ return new ArrayList <>(map .values ());
41+ }
42+ }
You canโt perform that action at this time.
0 commit comments