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+ /**
2+ * input str์ ๊ธธ์ด๊ฐ n์ด๊ณ , ๊ฐ str ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ m๋ผ ํ ๋
3+ *
4+ * TC : O(n * m log n)
5+ * - n ๋ฒ ์ํํ ๋๋ง๋ค ๊ฐ ๋จ์ด ์ ๋ ฌ์ m log m ๋งํผ ์์๋๋ฏ๋ก O(n * m log m)
6+ * SC : O(n * m)
7+ * - n * m ๋งํผ์ char ๋ฐฐ์ด ๊ณต๊ฐ์ key ๊ฐ ์์ฑ์ผ๋ก ์ฌ์ฉ
8+ * - ์ ๋ต Map ์์ ์
๋ ฅ ํฌ๊ธฐ n๋งํผ์ ๊ณต๊ฐ ์ ์ฌ์ฉ
9+ * - ๋ฐ๋ผ์ (n * m) + n ์ฆ O(n * m) ๋งํผ์ ๊ณต๊ฐ ์ฌ์ฉ
10+ */
11+ class Solution {
12+ public List <List <String >> groupAnagrams (String [] strs ) {
13+ Map <String , List <String >> answer = new HashMap <>();
14+
15+ for (String str : strs ) {
16+ char [] c = str .toCharArray ();
17+ Arrays .sort (c );
18+ String key = String .valueOf (c );
19+ answer .computeIfAbsent (key , k -> new ArrayList <>()).add (str );
20+ }
21+
22+ return new ArrayList <>(answer .values ());
23+ }
24+ }
You canโt perform that action at this time.
0 commit comments