
class Solution {
public List<String> commonChars(String[] words) {
List<String> list=new ArrayList<>();
for (int i = 0; i <words[0].length() ; i++) {
char c=words[0].charAt(i);
for (int j = 1; j <words.length ; j++) {
if(!words[j].contains(""+c)){
break;
}else if(j==words.length-1){
list.add(""+c);
}
words[j]=words[j].replaceFirst(c+"","");
}
}
return list;
}
}
该博客介绍了一个Java方法,用于找出多个字符串数组中的公共字符。通过遍历第一个字符串并检查其他字符串是否包含当前字符,如果所有字符串都包含该字符,则将其添加到结果列表中。该方法对于比较和分析字符串集合中的共性非常有用。
2415

被折叠的 条评论
为什么被折叠?



