Find Common Characters

找到n个string之间的共享字符. 既然是共享毕竟每两个都有. 所以先count一个string的字符, 然后算min找到共享.

class Solution {
    public List<String> commonChars(String[] A) {
        List<String> res = new ArrayList<String>();
        int[] count = count(A[0]); // count first string
        for(int j = 1 ; j < A.length; j++) { 
            int[] c = count(A[j]);
            for(int i = 0; i < 26; i++) {
                count[i] = Math.min(c[i], count[i]); //find the min of current count and new string count
            }
        }
        for(int i = 0 ; i < 26; i++) { // output
            if(count[i] != 0){
                for(int j = 0; j < count[i]; j++){
                    res.add(String.valueOf((char)(i + 'a')));
                }
            }
        }
        return res;
    }
    
    private int[] count(String s) { // count number of char in string
        int[] res = new int[26];
        for(int i = 0; i < s.length(); i++) {
            res[s.charAt(i) - 'a'] ++;
        }
        return res;
    }
}