@@ -180,77 +180,34 @@ use std::collections::HashMap;
180180
181181impl Solution {
182182 pub fn group_anagrams (strs : Vec <String >) -> Vec <Vec <String >> {
183- let mut map = HashMap :: new ();
183+ let mut d = HashMap :: new ();
184184 for s in strs {
185- let key = {
186- let mut arr = s . chars (). collect :: <Vec <char >>();
187- arr . sort ();
188- arr . iter (). collect :: <String >()
189- };
190- let val = map . entry (key ). or_insert (vec! []);
191- val . push (s );
185+ let mut t : Vec <char > = s . chars (). collect ();
186+ t . sort_unstable ();
187+ let k : String = t . into_iter (). collect ();
188+ d . entry (k ). or_insert_with (Vec :: new ). push (s );
192189 }
193- map . into_iter () . map ( | ( _ , v ) | v ). collect ()
190+ d . into_values ( ). collect ()
194191 }
195192}
196193```
197194
198195#### C#
199196
200197``` cs
201- using System .Collections .Generic ;
202-
203- public class Comparer : IEqualityComparer <string >
204- {
205- public bool Equals (string left , string right )
206- {
207- if (left .Length != right .Length ) return false ;
208-
209- var leftCount = new int [26 ];
210- foreach (var ch in left )
211- {
212- ++ leftCount [ch - 'a' ];
213- }
214-
215- var rightCount = new int [26 ];
216- foreach (var ch in right )
217- {
218- var index = ch - 'a' ;
219- if (++ rightCount [index ] > leftCount [index ]) return false ;
220- }
221-
222- return true ;
223- }
224-
225- public int GetHashCode (string obj )
226- {
227- var hashCode = 0 ;
228- for (int i = 0 ; i < obj .Length ; ++ i )
229- {
230- hashCode ^= 1 << (obj [i ] - 'a' );
231- }
232- return hashCode ;
233- }
234- }
235-
236198public class Solution {
237199 public IList <IList <string >> GroupAnagrams (string [] strs ) {
238- var dict = new Dictionary <string , List <string >>(new Comparer ());
239- foreach (var str in strs )
240- {
241- List < string > list ;
242- if (! dict .TryGetValue (str , out list ))
243- {
244- list = new List <string >();
245- dict .Add (str , list );
200+ var d = new Dictionary <string , List <string >>();
201+ foreach (string s in strs ) {
202+ char [] t = s .ToCharArray ();
203+ Array .Sort (t );
204+ string k = new string (t );
205+ if (! d .ContainsKey (k )) {
206+ d [k ] = new List <string >();
246207 }
247- list .Add (str );
248- }
249- foreach (var list in dict .Values )
250- {
251- list .Sort ();
208+ d [k ].Add (s );
252209 }
253- return new List <IList <string >>(dict .Values );
210+ return new List <IList <string >>(d .Values );
254211 }
255212}
256213```
@@ -357,12 +314,61 @@ func groupAnagrams(strs []string) (ans [][]string) {
357314
358315``` ts
359316function groupAnagrams(strs : string []): string [][] {
360- const map = new Map <string , string []>();
361- for (const str of strs ) {
362- const k = str .split (' ' ).sort ().join (' ' );
363- map .set (k , (map .get (k ) ?? []).concat ([str ]));
317+ const d = new Map <string , string []>();
318+ for (const s of strs ) {
319+ const cnt = new Array (26 ).fill (0 );
320+ for (const c of s ) {
321+ cnt [c .charCodeAt (0 ) - ' a' .charCodeAt (0 )]++ ;
322+ }
323+ const key = cnt .join (' ,' );
324+ if (! d .has (key )) {
325+ d .set (key , []);
326+ }
327+ d .get (key )! .push (s );
328+ }
329+ return Array .from (d .values ());
330+ }
331+ ```
332+
333+ #### Rust
334+
335+ ``` rust
336+ use std :: collections :: HashMap ;
337+
338+ impl Solution {
339+ pub fn group_anagrams (strs : Vec <String >) -> Vec <Vec <String >> {
340+ let mut d = HashMap :: new ();
341+ for s in strs {
342+ let mut cnt = [0 ; 26 ];
343+ for c in s . chars () {
344+ cnt [(c as usize ) - ('a' as usize )] += 1 ;
345+ }
346+ d . entry (cnt ). or_insert_with (Vec :: new ). push (s );
347+ }
348+ d . into_values (). collect ()
349+ }
350+ }
351+ ```
352+
353+ #### C#
354+
355+ ``` cs
356+ public class Solution {
357+ public IList <IList <string >> GroupAnagrams (string [] strs ) {
358+ var d = new Dictionary <string , List <string >>();
359+ foreach (string s in strs ) {
360+ int [] cnt = new int [26 ];
361+ foreach (char c in s ) {
362+ cnt [c - 'a' ]++ ;
363+ }
364+ string key = string .Join (" ," , cnt );
365+ if (! d .ContainsKey (key )) {
366+ d [key ] = new List <string >();
367+ }
368+ d [key ].Add (s );
369+ }
370+ return new List <IList <string >>(d .Values );
364371 }
365- return [... map .values ()];
366372}
367373```
368374
0 commit comments