Skip to content

Commit f0f82f2

Browse files
committed
8-7-2020
1 parent 4529840 commit f0f82f2

3 files changed

+84
-0
lines changed

src/_0049_group_anagrams.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct Solution;
2+
3+
use std::collections::HashMap;
4+
5+
impl Solution {
6+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
7+
let mut h_map: HashMap<Vec<char>, Vec<String>> = HashMap::new();
8+
for str in strs {
9+
let mut str_: Vec<char> = str.chars().collect();
10+
str_.sort();
11+
let h_group = h_map.entry(str_).or_insert(Vec::new());
12+
(*h_group).push(str);
13+
}
14+
15+
let mut result: Vec<Vec<String>> = h_map.into_iter().map(|(_k, v)| v).collect();
16+
result.sort_by_key(|x| x.len());
17+
result
18+
}
19+
}
20+
21+
#[test]
22+
fn test() {
23+
assert_eq!(
24+
Solution::group_anagrams(vec_string!["eat", "tea", "tan", "ate", "nat", "bat"]),
25+
vec![
26+
vec_string!["bat"],
27+
vec_string!["tan", "nat"],
28+
vec_string!["eat", "tea", "ate"],
29+
]
30+
);
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn max_length(arr: Vec<String>) -> i32 {
5+
let arr: Vec<u32> = arr
6+
.into_iter()
7+
.filter_map(|s| {
8+
let mut word_bit: u32 = 0;
9+
for b in s.bytes() {
10+
let b_: u32 = 1 << (b - b'a');
11+
if b_ & word_bit != 0 {
12+
return None;
13+
} else {
14+
word_bit |= b_;
15+
}
16+
}
17+
Some(word_bit)
18+
})
19+
.collect();
20+
21+
let mut result: u32 = 0;
22+
Self::dfs(&arr, 0, &mut result, 0);
23+
result as i32
24+
}
25+
26+
fn dfs(arr: &Vec<u32>, index: usize, maximum: &mut u32, current_concat: u32) {
27+
if index == arr.len() {
28+
*maximum = (*maximum).max(current_concat.count_ones());
29+
} else {
30+
if arr[index] & current_concat == 0 {
31+
Self::dfs(arr, index + 1, maximum, current_concat | arr[index]);
32+
}
33+
Self::dfs(arr, index + 1, maximum, current_concat);
34+
}
35+
}
36+
}
37+
38+
#[test]
39+
fn test() {
40+
assert_eq!(Solution::max_length(vec_string!["un", "iq", "ue"]), 4);
41+
assert_eq!(
42+
Solution::max_length(vec_string!["cha", "r", "act", "ers"]),
43+
6
44+
);
45+
assert_eq!(
46+
Solution::max_length(vec_string!["abcdefghijklmnopqrstuvwxyz"]),
47+
26
48+
);
49+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ mod _0038_count_and_say;
4444
//
4545
mod _0046_permutations;
4646
//
47+
mod _0049_group_anagrams;
48+
//
4749
mod _0053_maximum_subarray;
4850
//
4951
mod _0056_merge_intervals;
@@ -184,6 +186,8 @@ mod _1021_remove_outermost_parentheses;
184186
//
185187
mod _1029_two_city_scheduling;
186188
//
189+
mod _1239_maximum_length_of_a_concatenated_string_with_unique_characters;
190+
//
187191
mod _1249_minimum_remove_to_make_valid_parentheses;
188192
//
189193
mod _1281_subtract_the_product_and_sum_of_digits_of_an_integer;

0 commit comments

Comments
 (0)