File tree 1 file changed +15
-13
lines changed 1 file changed +15
-13
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public List <String > commonChars (String [] A ) {
3
- int [][] counter = new int [A .length ][26 ];
4
- for (int i = 0 ; i < A .length ; i ++) {
5
- for (char c : A [i ].toCharArray ()) {
6
- counter [i ][c - 'a' ]++;
2
+ public List <String > commonChars (String [] words ) {
3
+ int [] commonFrequency = new int [26 ];
4
+ Arrays .fill (commonFrequency , Integer .MAX_VALUE );
5
+ for (String word : words ) {
6
+ int [] wordFreq = new int [26 ];
7
+ for (char c : word .toCharArray ()) {
8
+ wordFreq [c - 'a' ]++;
9
+ }
10
+ for (int i = 0 ; i < 26 ; i ++) {
11
+ commonFrequency [i ] = Math .min (commonFrequency [i ], wordFreq [i ]);
7
12
}
8
13
}
9
- List <String > list = new ArrayList <>();
14
+ List <String > result = new ArrayList <>();
10
15
for (int i = 0 ; i < 26 ; i ++) {
11
- int minCount = Integer .MAX_VALUE ;
12
- for (int j = 0 ; j < counter .length ; j ++) {
13
- minCount = Math .min (minCount , counter [j ][i ]);
14
- }
15
- while (minCount -- > 0 ) {
16
- list .add (String .valueOf ((char ) (97 + i )));
16
+ int count = commonFrequency [i ];
17
+ while (count -- > 0 ) {
18
+ result .add (String .valueOf ((char ) (97 + i )));
17
19
}
18
20
}
19
- return list ;
21
+ return result ;
20
22
}
21
23
}
You can’t perform that action at this time.
0 commit comments