-
Notifications
You must be signed in to change notification settings - Fork 0
/
1002 Find Common Characters.c
50 lines (44 loc) · 1.13 KB
/
1002 Find Common Characters.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
char ** commonChars(char ** words, int wordsSize, int* returnSize)
{
char ** ret_arr = (char **)malloc(sizeof(char *) * 10000);
int ret_arr_index = 0;
int rows = 0;
int i=0;
int min_count = INT_MAX;
int cur_count = 0;
for(i='a'; i<='z'; i++)
{
min_count = INT_MAX;
cur_count = 0;
for(rows=0; rows<wordsSize; rows++)
{
int j=0;
cur_count = 0;
while(words[rows][j] != '\0')
{
if(words[rows][j] == i)
{
cur_count++;
}
j++;
}
if(cur_count <= min_count)
{
min_count = cur_count;
}
}
while(min_count > 0)
{
char * new_arr = (char *)malloc(sizeof(char) * 2);
new_arr[0] = i;
new_arr[1] = '\0';
ret_arr[ret_arr_index++] = new_arr;
min_count--;
}
}
*returnSize = ret_arr_index;
return ret_arr;
}