aboutsummaryrefslogtreecommitdiff
path: root/group_anagrams.rs
blob: 212fc153b77145e18eca59ca7f058eea7019c82f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use std::collections::HashMap;
struct Solution;

impl Solution {
    pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
        let mut groups = HashMap::new();
        for s in strs {
            let hash = s.bytes().fold([0;26], |mut h, b| {
                h[(b - b'a') as usize] += 1; h
            });
            groups.entry(hash).or_insert_with(Vec::new).push(s);
        }
        groups.into_values().collect()
    }
}