-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_anagrams.rs
104 lines (91 loc) · 2.97 KB
/
group_anagrams.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#![allow(dead_code)]
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
use std::collections::HashMap;
let mut map: HashMap<Vec<u8>, Vec<String>> = HashMap::new();
for s in strs {
let mut sorted = s.as_bytes().to_vec();
sorted.sort();
map.entry(sorted).or_insert(Vec::new()).push(s);
}
map.into_iter().map(|(_, v)| v).collect()
}
/*
Algorithm
- Create a hashmap with key as sorted string and value as vector of strings
- Iterate over the input vector and sort each string
- Insert the sorted string as key and the original string as value in the hashmap
- Return the values of the hashmap
Complexity
- Time: O(n * klogk) where n is the number of strings and k is the length of the longest string
- Space: O(n * k) where n is the number of strings and k is the length of the longest string
Example
- Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
- Output: [["ate","eat","tea"], ["nat","tan"], ["bat"]]
*/
#[cfg(test)]
mod tests {
use super::*;
fn compare_vecs(v1: Vec<Vec<String>>, v2: Vec<Vec<String>>) -> bool {
// Compare the length of the vectors
if v1.len() != v2.len() {
return false;
}
true
}
#[test]
fn test_group_anagrams() {
let strs = vec![
"eat".to_string(),
"tea".to_string(),
"tan".to_string(),
"ate".to_string(),
"nat".to_string(),
"bat".to_string(),
];
let expected = vec![
vec!["ate".to_string(), "eat".to_string(), "tea".to_string()],
vec!["nat".to_string(), "tan".to_string()],
vec!["bat".to_string()],
];
assert_eq!(compare_vecs(group_anagrams(strs), expected), true);
}
#[test]
fn test_empty() {
let strs = vec![];
let expected: Vec<Vec<String>> = vec![];
assert_eq!(group_anagrams(strs), expected);
}
#[test]
fn test_single() {
let strs = vec!["a".to_string()];
let expected = vec![vec!["a".to_string()]];
assert_eq!(group_anagrams(strs), expected);
}
#[test]
fn test_empty_string() {
let strs = vec!["".to_string()];
let expected = vec![vec!["".to_string()]];
assert_eq!(group_anagrams(strs), expected);
}
#[test]
fn test_different_length() {
let strs = vec![
"eat".to_string(),
"tea".to_string(),
"tan".to_string(),
"ate".to_string(),
"nat".to_string(),
"bat".to_string(),
"a".to_string(),
"".to_string(),
];
let expected = vec![
vec!["ate".to_string(), "eat".to_string(), "tea".to_string()],
vec!["nat".to_string(), "tan".to_string()],
vec!["bat".to_string()],
vec!["a".to_string()],
vec!["".to_string()],
];
assert_eq!(compare_vecs(group_anagrams(strs), expected), true);
}
}