aboutsummaryrefslogtreecommitdiff
path: root/max_len_concat_str.rs
blob: 8ce0c2beda88cbc7ff3b961bbb452d5ff78390f6 (plain)
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
struct Solution;

impl Solution {
    pub fn max_length(arr: Vec<String>) -> i32 {
        let mut ans: u32 = 0;
        let mut unique: Vec<u32> = Vec::new();
        for s in arr {
            let bin: u32 = s.bytes().map(|c| 1 << (c - b'a')).sum();
            if bin.count_ones() == s.len() as u32 {
                unique.push(bin);
            }
        }
        let mut concat: Vec<u32> = vec![0];
        for u in unique {
            for i in 0..concat.len() {
                if (concat[i] & u).count_ones() == 0 {
                    concat.push(concat[i] | u);
                    ans = ans.max(concat[i].count_ones() + u.count_ones());
                }
            }
        }
        ans as i32
    }
}

fn main() {
    let arr = vec!["un".to_string(),"iq".to_string(),"ue".to_string()];
    print!("{}", Solution::max_length(arr));
}