给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。
你可以按任意顺序返回答案。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-common-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution { public List<String> commonChars(String[] A) { int[] count=new int[27]; List<String> ans = new ArrayList<String>(); for(int i=0;i<27;++i){ count[i]=100; } int[] temp=new int[27]; for(String x:A){ char[] a=x.toCharArray(); for(int i=0;i<a.length;++i){ temp[a[i]-'a']++; } for(int k=0;k<27;++k){ if(temp[k]<count[k]){ count[k]=temp[k]; } temp[k]=0; } } for(int i=0;i<27;++i){ for(int k=count[i];k>0;--k){ ans.add(String.valueOf((char)(i+'a'))); } } return ans; } }
x.toCharArrays():把string转换成字符数组。
小写字母减去‘a’是其所对应的字母表顺序。
String.valueof()返回一个string;