File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ // 1. 객체사용
2+ function groupAnagrams1 ( strs : string [ ] ) : string [ ] [ ] {
3+ let anagramGroups : Record < string , string [ ] > = { } ;
4+ for ( const word of strs ) {
5+ const sortedKey = [ ...word ] . sort ( ) . join ( "" ) ;
6+
7+ if ( sortedKey in anagramGroups ) {
8+ anagramGroups [ sortedKey ] . push ( word ) ;
9+ } else {
10+ anagramGroups [ sortedKey ] = [ word ] ;
11+ }
12+ }
13+
14+ return Object . values ( anagramGroups ) ;
15+ } ;
16+
17+ // 2. Map 사용
18+ function groupAnagrams2 ( strs : string [ ] ) : string [ ] [ ] {
19+ let anagramGroups = new Map < string , string [ ] > ( ) ;
20+ for ( const word of strs ) {
21+ const key = [ ...word ] . sort ( ) . join ( "" ) ;
22+
23+ if ( anagramGroups . has ( key ) ) {
24+ anagramGroups . get ( key ) ?. push ( word ) ;
25+ } else {
26+ anagramGroups . set ( key , [ word ] ) ;
27+ }
28+ }
29+
30+ return [ ...anagramGroups . values ( ) ] ;
31+ } ;
You can’t perform that action at this time.
0 commit comments