1002. Find Common Characters

Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates).  For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.

You may return the answer in any order.

 

Example 1:

Input: ["bella","label","roller"]
Output: ["e","l","l"]

Example 2:

Input: ["cool","lock","cook"]
Output: ["c","o"]

 

Note:

  1. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 100
  3. A[i][j] is a lowercase letter

Rust Solution

struct Solution;

use std::usize;

impl Solution {
    fn common_chars(a: Vec<String>) -> Vec<String> {
        let n = a.len();
        let mut counts: Vec<Vec<usize>> = vec![vec![0; 256]; n];
        for i in 0..n {
            let w = &a[i];
            for c in w.chars() {
                counts[i][c as usize] += 1;
            }
        }
        let mut res: Vec<String> = vec![];
        for i in 0..26 {
            let c: u8 = b'a' + i;
            let mut min = usize::MAX;
            for j in 0..n {
                let count = counts[j][c as usize];
                min = usize::min(count, min);
            }
            for _ in 0..min {
                res.push(format!("{}", c as char))
            }
        }
        res
    }
}

#[test]
fn test() {
    let a: Vec<String> = vec_string!["bella", "label", "roller"];
    let b: Vec<String> = vec_string!["e", "l", "l"];
    assert_eq!(Solution::common_chars(a), b);

    let a: Vec<String> = vec_string!["cool", "lock", "cook"];
    let b: Vec<String> = vec_string!["c", "o"];
    assert_eq!(Solution::common_chars(a), b);
}

Having problems with this solution? Click here to submit an issue on github.