1. 程式人生 > 其它 >#力扣 LeetCode1002. 查詢常用字元 @FDDLC

#力扣 LeetCode1002. 查詢常用字元 @FDDLC

技術標籤:演算法&資料結構

題目描述:

https://leetcode-cn.com/problems/find-common-characters/

Java程式碼:

import java.util.LinkedList;
import java.util.List;

class Solution { //僅有小寫字母組成的字串陣列 A,你可以按任意順序返回答案
    public List<String> commonChars(String[] A) { //1 <= A.length <= 100,1 <= A[i].length <= 100,A[i][j] 是小寫字母
        int[] count=new int[26];
        for(int i=0;i<A[0].length();i++)count[A[0].charAt(i)-'a']++;
        for(int i=1;i<A.length;i++){
            int[] temp=new int[count.length];
            for(int j=0;j<A[i].length();j++)temp[A[i].charAt(j)-'a']++;
            for(int k=0;k<count.length;k++)count[k]=Math.min(count[k],temp[k]);
        }
        List<String> answer=new LinkedList<>();
        for(int i=0;i<count.length;i++){
            for(int j=0;j<count[i];j++)answer.add(new String(""+(char)(i+'a')));
        }
        return answer;
    }
}