题目标签:Array, Hash Table
Java Solution:
完成日期:03/08/2019
关键点:对于每一个新的string,把common array 里面的次数进行更新,取最小的次数,排除不是 common 的 字符。
class Solution { public List<String> commonChars(String[] A) { List<String> result = new ArrayList<>(); int [] commonCharsCount = new int[26]; Arrays.fill(commonCharsCount, Integer.MAX_VALUE); // iterate each string in A for(String str : A) { int [] tempCharsCount = new int[26]; // count char for this string for(char c : str.toCharArray()) tempCharsCount[c - 'a']++; // update the commonCharsCount for(int i=0; i<commonCharsCount.length; i++) commonCharsCount[i] = Math.min(commonCharsCount[i], tempCharsCount[i]); } // iterate commonCharsCount to add each char for(int i=0; i<commonCharsCount.length; i++) { while(commonCharsCount[i] > 0) { result.add("" + (char)('a' + i)); commonCharsCount[i]--; } } return result; } }
参考资料:https://leetcode.com/problems/find-common-characters/discuss/?currentPage=1&orderBy=recent_activity&query=
题目来源:https://leetcode.com/