SHARE
TWEET

Untitled

a guest Mar 26th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2.     // March 26th 2019
  3.     public List<String> commonChars(String[] A) {
  4.         List<String> result = new ArrayList<>();
  5.         int[] minFreq = new int[26];
  6.         int[] numWords = new int[26];
  7.        
  8.        
  9.         for (int k = 0; k < A.length; k++) {
  10.             String s = A[k];
  11.             int[] currFreq = new int[26];
  12.             for (int j = 0; j < s.length(); j++) {
  13.                 currFreq[s.charAt(j) - 'a']++;
  14.             }
  15.             for (int i = 0; i < 26; i++) {
  16.                 if (k == 0) {
  17.                     minFreq[i] = currFreq[i];
  18.                 }
  19.                 if (minFreq[i] > 0 && currFreq[i] > 0) {
  20.                     numWords[i]++;
  21.                     minFreq[i] = Math.min(minFreq[i], currFreq[i]);
  22.                 }
  23.             }
  24.         }
  25.         //System.out.println();
  26.         for (int i = 0; i < 26; i++) {
  27.             if (numWords[i] == A.length) {
  28.                 for (int j = 0; j < minFreq[i]; j++) {
  29.                     result.add(String.valueOf((char)(i + 'a')));
  30.                 }
  31.             }
  32.         }
  33.         return result;
  34.     }
  35. }
  36. /*
  37. ["bella","label","roller"]
  38. ["cool","lock","cook"]
  39. ["a", "b", "b"]
  40. ["aaa", "aaa"]
  41. ["b"]
  42. []
  43. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top