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