File tree 1 file changed +35
-0
lines changed 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์๊ฐ ๋ณต์ก๋: O (m * n)
3
+ * - m: ๋ฌธ์์ด ๊ฐ์
4
+ * - n: ๊ฐ ๋ฌธ์์ด ๊ธธ์ด
5
+ * - ๊ฐ ๋ฌธ์์ด์์ ๋น๋์ ๊ณ์ฐ์ ์ํด O(n)์ด ์์๋๋๋ฐ, ์ด๋ฅผ ๊ฐ ๋ฌธ์์ด๋ง๋ค ์งํ
6
+ *
7
+ * ๊ณต๊ฐ ๋ณต์ก๋: O (m * n)
8
+ * - m: ๋ฌธ์์ด ๊ฐ์
9
+ * - n: ๊ฐ ๋ฌธ์์ด ๊ธธ์ด
10
+ * - ์ฃผ์ด์ง ๋ชจ๋ ๋ฌธ์์ด์ด ๋ชจ๋ ์๋ก anagram์ ๊ตฌ์ฑํ์ง ๋ชปํ ๊ฒฝ์ฐ, ๋ชจ๋ ๋ฌธ์์ด ์๋งํผ ํ์ํจ
11
+ */
12
+ class Solution {
13
+ public List <List <String >> groupAnagrams (String [] strs ) {
14
+ Map <String , List <String >> map = new HashMap <>();
15
+
16
+ for (String str : strs ) {
17
+ int [] count = new int [26 ];
18
+ for (int i = 0 ; i < str .length (); i ++) {
19
+ count [str .charAt (i ) - 'a' ]++;
20
+ }
21
+
22
+ StringBuilder sb = new StringBuilder ();
23
+ for (int i = 0 ; i < 26 ; i ++) {
24
+ sb .append ("^" ).append (count [i ]);
25
+ }
26
+ String candidate = sb .toString ();
27
+ if (!map .containsKey (candidate )) {
28
+ map .put (candidate , new ArrayList <>());
29
+ }
30
+ map .get (candidate ).add (str );
31
+ }
32
+
33
+ return new ArrayList <>(map .values ());
34
+ }
35
+ }
You canโt perform that action at this time.
0 commit comments