Leetcode 1002 Count and Say Solution in java | Hindi Coding Community

0


 

Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.


Example 1:


Input: words = ["bella","label","roller"]

Output: ["e","l","l"]




public List<String> commonChars(String[] A) {
List<String> res=new ArrayList<String>();
int[] min=new int[26];
for(int i=0;i<26;i++){
min[i]=Integer.MAX_VALUE;
}
for(String s:A){
int[] cur=new int[26];
char[] ca=s.toCharArray();
for(char c:ca){
cur[c -'a']++;
}
for(int i=0;i<26;i++){
min[i]=Math.min(min[i],cur[i]);
}
}
for(int i=0;i<26;i++){
for(int j=0;j<min[i];j++){
res.add(String.valueOf((char)(i+'a')));
}
}
return res;
}


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !