#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.
这道题的意思是,找出A中每个单词都共有的字母。所以可以以第一个单词为标杆,第一个单词中的每个字母是否都出现在了后面的单词中,出现了就是结果所需要的。这里需要注意,判断过的单词就要让它为空,一面影响下一次遍历的判断。用x.replace(letter,"")来置空。
/**
* @param {string[]} A
* @return {string[]}
*/
var commonChars = function(A) {
var result=[];
if(A===[]) {
return result;
}
for(let letter of A[0]) {
var flag=true;
for(let i=0;i<A.length;i++) {
if(A[i].indexOf(letter) >-1) {
A[i]=A[i].replace(letter,"");
}
else {
flag=false;
}
}
if(flag===true) {
result.push(letter);
}
}
return result;
};