File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์ ๋๊ทธ๋จ๋ผ๋ฆฌ ๋ฌถ์ด์ ๋ฐํํ๋ ํจ์
3+ * @param {string[] } strs
4+ * @return {string[][] }
5+ */
6+ const groupAnagrams = function ( strs ) {
7+ // ํ์ด 1
8+ // ์๊ฐ๋ณต์ก๋: O(n*s) (n: strs.length, s: str.length)
9+ // ๊ณต๊ฐ๋ณต์ก๋: O(n)
10+ function groupManually ( ) {
11+ const groups = { } ;
12+
13+ strs . forEach ( ( str ) => {
14+ const key = [ ...str ] . sort ( ) . join ( '' ) ;
15+ if ( ! groups [ key ] ) groups [ key ] = [ ] ;
16+ groups [ key ] . push ( str ) ;
17+ } ) ;
18+
19+ return Object . values ( groups ) ;
20+ }
21+
22+ // ํ์ด 2
23+ // ์๊ฐ๋ณต์ก๋: O(n*s) (n: strs.length, s: str.length)
24+ // ๊ณต๊ฐ๋ณต์ก๋: O(n)
25+ function groupByAnagram ( ) {
26+ const result = Object . groupBy ( strs , ( str ) => [ ...str ] . sort ( ) . join ( '' ) ) ;
27+ return Object . values ( result ) ;
28+ }
29+
30+ return groupByAnagram ( ) ;
31+ } ;
32+
You canโt perform that action at this time.
0 commit comments