From e90ea1152ceb61670a245a5bac60c63fdcc272ed Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Sun, 12 May 2024 12:19:06 +0800 Subject: [PATCH] leetcode: Add 0049 --- src/leetcode/0049.group-anagrams/Cargo.toml | 7 ++ src/leetcode/0049.group-anagrams/index.md | 4 ++ src/leetcode/0049.group-anagrams/src/main.rs | 76 ++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 src/leetcode/0049.group-anagrams/Cargo.toml create mode 100644 src/leetcode/0049.group-anagrams/index.md create mode 100644 src/leetcode/0049.group-anagrams/src/main.rs diff --git a/src/leetcode/0049.group-anagrams/Cargo.toml b/src/leetcode/0049.group-anagrams/Cargo.toml new file mode 100644 index 000000000..f2950c1a5 --- /dev/null +++ b/src/leetcode/0049.group-anagrams/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-0049-group-anagrams" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/0049.group-anagrams/index.md b/src/leetcode/0049.group-anagrams/index.md new file mode 100644 index 000000000..77a618ba8 --- /dev/null +++ b/src/leetcode/0049.group-anagrams/index.md @@ -0,0 +1,4 @@ + +# + +[问题描述](../problems/) diff --git a/src/leetcode/0049.group-anagrams/src/main.rs b/src/leetcode/0049.group-anagrams/src/main.rs new file mode 100644 index 000000000..7cfbdee07 --- /dev/null +++ b/src/leetcode/0049.group-anagrams/src/main.rs @@ -0,0 +1,76 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +use std::collections::HashMap; + +// HashTable +// 将每个字符串按升序排序, 作为它的索引. +pub fn group_anagrams1(strs: Vec) -> Vec> { + debug_assert!(!strs.is_empty()); + + let mut map: HashMap> = HashMap::new(); + for s in strs.into_iter() { + // 构造字符串的索引 + let mut chars_vec: Vec = s.chars().collect(); + chars_vec.sort_unstable(); + let index: String = chars_vec.into_iter().collect(); + // 将 (字符串索引, 字符串) 插入到表哈稀表中. + map.entry(index).or_default().push(s); + } + + //map.into_values().collect() + //map.into_iter().map(|(_index, strs)| strs).collect() + + // 为了测试, 我们对结果进行一下排序. + let mut out: Vec> = map + .into_values() + .map(|mut strs| { + strs.sort_unstable(); + strs + }) + .collect(); + out.sort_by_key(|v| v.len()); + out +} + +pub type SolutionFn = fn(Vec) -> Vec>; + +fn check_solution(func: SolutionFn) { + let strs = vec![ + "eat".to_owned(), + "tea".to_owned(), + "tan".to_owned(), + "ate".to_owned(), + "nat".to_owned(), + "bat".to_owned(), + ]; + let exp = vec![ + vec!["bat".to_owned()], + vec!["nat".to_owned(), "tan".to_owned()], + vec!["ate".to_owned(), "eat".to_owned(), "tea".to_owned()], + ]; + assert_eq!(func(strs), exp); + + let strs = vec!["".to_owned()]; + let exp = vec![vec!["".to_owned()]]; + assert_eq!(func(strs), exp); + + let strs = vec!["a".to_owned()]; + let exp = vec![vec!["a".to_owned()]]; + assert_eq!(func(strs), exp); +} + +fn main() { + check_solution(group_anagrams1); +} + +#[cfg(test)] +mod tests { + use super::{check_solution, group_anagrams1}; + + #[test] + fn test_group_anagrams1() { + check_solution(group_anagrams1); + } +}