Skip to content

Commit b65a3c8

Browse files
authored
group anagrams solution
1 parent a4e6944 commit b65a3c8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

group-anagrams/yhkee0404.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)