rustgym/leetcode/
_893_groups_of_special_equivalent_string.rs1struct Solution;
2
3use std::collections::BTreeMap;
4use std::collections::HashSet;
5
6#[derive(Debug, PartialEq, Eq, Clone, Hash)]
7struct Count {
8 even: BTreeMap<char, usize>,
9 odd: BTreeMap<char, usize>,
10}
11
12impl Count {
13 fn new(s: String) -> Self {
14 let mut even: BTreeMap<char, usize> = BTreeMap::new();
15 let mut odd: BTreeMap<char, usize> = BTreeMap::new();
16 for (i, c) in s.chars().enumerate() {
17 if i % 2 == 0 {
18 *even.entry(c).or_default() += 1;
19 } else {
20 *odd.entry(c).or_default() += 1;
21 }
22 }
23 Count { even, odd }
24 }
25}
26
27impl Solution {
28 fn num_special_equiv_groups(a: Vec<String>) -> i32 {
29 let mut hs: HashSet<Count> = HashSet::new();
30 for s in a {
31 hs.insert(Count::new(s));
32 }
33 hs.len() as i32
34 }
35}
36
37#[test]
38fn test() {
39 let a: Vec<String> = vec_string!["a", "b", "c", "a", "c", "c"];
40 assert_eq!(Solution::num_special_equiv_groups(a), 3);
41 let a: Vec<String> = vec_string!["aa", "bb", "ab", "ba"];
42 assert_eq!(Solution::num_special_equiv_groups(a), 4);
43 let a: Vec<String> = vec_string!["abc", "acb", "bac", "bca", "cab", "cba"];
44 assert_eq!(Solution::num_special_equiv_groups(a), 3);
45 let a: Vec<String> = vec_string!["abcd", "cdab", "adcb", "cbad"];
46 assert_eq!(Solution::num_special_equiv_groups(a), 1);
47}