File tree 4 files changed +93
-0
lines changed
lcci/10.02.Group Anagrams
4 files changed +93
-0
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,40 @@ func groupAnagrams(strs []string) [][]string {
118
118
}
119
119
```
120
120
121
+ ### ** TypeScript**
122
+
123
+ ``` ts
124
+ function groupAnagrams(strs : string []): string [][] {
125
+ const map = new Map <string , string []>();
126
+ for (const s of strs ) {
127
+ const k = s .split (' ' ).sort ().join ();
128
+ map .set (k , (map .get (k ) || []).concat ([s ]));
129
+ }
130
+ return [... map .values ()];
131
+ }
132
+ ```
133
+
134
+ ### ** Rust**
135
+
136
+ ``` rust
137
+ use std :: collections :: HashMap ;
138
+
139
+ impl Solution {
140
+ pub fn group_anagrams (strs : Vec <String >) -> Vec <Vec <String >> {
141
+ let mut map = HashMap :: new ();
142
+ for s in strs {
143
+ let key = {
144
+ let mut cs = s . chars (). collect :: <Vec <char >>();
145
+ cs . sort ();
146
+ cs . iter (). collect :: <String >()
147
+ };
148
+ map . entry (key ). or_insert (vec! []). push (s );
149
+ }
150
+ map . into_values (). collect ()
151
+ }
152
+ }
153
+ ```
154
+
121
155
### ** ...**
122
156
123
157
```
Original file line number Diff line number Diff line change @@ -115,6 +115,41 @@ func groupAnagrams(strs []string) [][]string {
115
115
}
116
116
```
117
117
118
+ ### ** TypeScript**
119
+
120
+ ``` ts
121
+ function groupAnagrams(strs : string []): string [][] {
122
+ const map = new Map <string , string []>();
123
+ for (const s of strs ) {
124
+ const k = s .split (' ' ).sort ().join ();
125
+ map .set (k , (map .get (k ) || []).concat ([s ]));
126
+ }
127
+ return [... map .values ()];
128
+ }
129
+ ```
130
+
131
+ ### ** Rust**
132
+
133
+ ``` rust
134
+ use std :: collections :: HashMap ;
135
+
136
+ impl Solution {
137
+ pub fn group_anagrams (strs : Vec <String >) -> Vec <Vec <String >> {
138
+ let mut map = HashMap :: new ();
139
+ for s in strs {
140
+ let key = {
141
+ let mut cs = s . chars (). collect :: <Vec <char >>();
142
+ cs . sort ();
143
+ cs . iter (). collect :: <String >()
144
+ };
145
+ map . entry (key ). or_insert (vec! []). push (s );
146
+ }
147
+ map . into_values (). collect ()
148
+ }
149
+ }
150
+ ```
151
+
152
+
118
153
### ** ...**
119
154
120
155
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
3
+ impl Solution {
4
+ pub fn group_anagrams ( strs : Vec < String > ) -> Vec < Vec < String > > {
5
+ let mut map = HashMap :: new ( ) ;
6
+ for s in strs {
7
+ let key = {
8
+ let mut cs = s. chars ( ) . collect :: < Vec < char > > ( ) ;
9
+ cs. sort ( ) ;
10
+ cs. iter ( ) . collect :: < String > ( )
11
+ } ;
12
+ map. entry ( key) . or_insert ( vec ! [ ] ) . push ( s) ;
13
+ }
14
+ map. into_values ( ) . collect ( )
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ function groupAnagrams ( strs : string [ ] ) : string [ ] [ ] {
2
+ const map = new Map < string , string [ ] > ( ) ;
3
+ for ( const s of strs ) {
4
+ const k = s . split ( '' ) . sort ( ) . join ( ) ;
5
+ map . set ( k , ( map . get ( k ) || [ ] ) . concat ( [ s ] ) ) ;
6
+ }
7
+ return [ ...map . values ( ) ] ;
8
+ }
You can’t perform that action at this time.
0 commit comments