Skip to content

Commit 8a4a45d

Browse files
authored
Merge pull request #2490 from DaleStudy/week5
[DaleSeo] WEEK 05 Solutions
2 parents 720b898 + 72ac8bb commit 8a4a45d

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

group-anagrams/DaleSeo.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC: O(n * k) where n = number of strings, k = max string length
2+
// SC: O(n * k)
3+
use std::collections::HashMap;
4+
5+
impl Solution {
6+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
7+
let mut anagrams: HashMap<[u8; 26], Vec<String>> = HashMap::new();
8+
for s in strs {
9+
let mut count = [0u8; 26];
10+
for b in s.bytes() {
11+
count[(b - b'a') as usize] += 1;
12+
}
13+
anagrams.entry(count).or_default().push(s);
14+
}
15+
anagrams.into_values().collect()
16+
}
17+
}

0 commit comments

Comments
 (0)