File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ use itertools:: Itertools ;
2
+
3
+ impl Solution {
4
+ pub fn group_anagrams ( strs : Vec < String > ) -> Vec < Vec < String > > {
5
+ let sorted_strings: Vec < String > = strs
6
+ . iter ( )
7
+ . map ( |s| {
8
+ s. chars ( )
9
+ . sorted ( )
10
+ . collect ( )
11
+ } ) . collect ( ) ;
12
+ let inverted_sorted_strings: Vec < usize > = ( 0 ..sorted_strings. len ( ) )
13
+ . sorted_by_key ( |& i| & sorted_strings[ i] )
14
+ . collect ( ) ;
15
+ let mut ans: Vec < Vec < String > > = vec ! [ ] ;
16
+ let mut i = 0 ;
17
+ let mut j;
18
+ while i != sorted_strings. len ( ) {
19
+ let mut u: Vec < String > = vec ! [ ] ;
20
+ j = i;
21
+ while j == i || j != inverted_sorted_strings. len ( ) && sorted_strings[ inverted_sorted_strings[ i] ] == sorted_strings[ inverted_sorted_strings[ j] ] {
22
+ u. push ( strs[ inverted_sorted_strings[ j] ] . clone ( ) ) ;
23
+ j += 1 ;
24
+ }
25
+ ans. push ( u) ;
26
+ i = j
27
+ }
28
+ ans
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments