給定僅有小寫字母組成的字符串?dāng)?shù)組 A,返回列表中的每個字符串中都顯示的全部字符(包括重復(fù)字符)組成的列表。例如,如果一個字符在每個字符串中出現(xiàn) 3 次,但不是 4 次,則需要在最終答案中包含該字符 3 次。
你可以按任意順序返回答案。
示例 1:
輸入:["bella","label","roller"]
輸出:["e","l","l"]
示例 2:
輸入:["cool","lock","cook"]
輸出:["c","o"]
提示:
1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j] 是小寫字母
class Solution {
public List<String> commonChars(String[] A) {
List<String>result = new ArrayList();
int length = A.length;
String firstStr = A[0];
int strLength = firstStr.length();
for(int i = 0; i < strLength; i++){
String indexStr = firstStr.substring(i,i + 1);
int count = 0;
for(int j = 1; j < length; j++){
String str = A[j];
if(str.contains(indexStr)){
count++;
A[j] = str.replaceFirst(indexStr,"");//有這個字母就清除掉一個
} else {
break;
}
}
if(count == length -1){//每一個都有
result.add(indexStr);
}
}
return result;
}
}