1002. 查找常用字符

  1. class Solution {
  2. public List<String> commonChars(String[] A) {
  3. List<String> ans = new ArrayList<>();
  4. if (A == null || A.length == 0)
  5. return ans;
  6. int[] hash = new int[26];
  7. for (int i = 0; i < A[0].length(); i++) {
  8. ++hash[A[0].charAt(i) - 'a'];
  9. }
  10. int[] other = new int[26];
  11. for (int i = 1; i < A.length; i++) {
  12. Arrays.fill(other, 0);
  13. for (int j = 0; j < A[i].length(); j++) {
  14. ++other[A[i].charAt(j) - 'a'];
  15. };
  16. for (int index = 0; index < 26; index++) {
  17. hash[index] = Math.min(hash[index], other[index]);
  18. }
  19. }
  20. for (int i = 0; i < 26; i++) {
  21. while (hash[i] != 0) {
  22. ans.add(new String("" + (char)('a' + i)));
  23. --hash[i];
  24. }
  25. }
  26. return ans;
  27. }
  28. }