forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1189.c
51 lines (47 loc) · 1.06 KB
/
1189.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
51
int maxNumberOfBalloons(char *text)
{
/*
0 -> b,
1 -> a,
2 -> l,
3 -> o,
4 -> n
*/
int count_letters[5] = {0};
int i, min_counter_ballons;
for (char *ptr = text; *ptr; ptr++)
{
if (*ptr == 'b')
{
count_letters[0]++;
}
else if (*ptr == 'a')
{
count_letters[1]++;
}
else if (*ptr == 'l')
{
count_letters[2]++;
}
else if (*ptr == 'o')
{
count_letters[3]++;
}
else if (*ptr == 'n')
{
count_letters[4]++;
}
}
/* Divide by 2 the repeted letters */
count_letters[2] /= 2;
count_letters[3] /= 2;
/* Max number of times which we can write ballon is equal to min value of
* letters on count_letter */
min_counter_ballons = count_letters[0];
for (i = 1; i < 5; i++)
{
if (count_letters[i] < min_counter_ballons)
min_counter_ballons = count_letters[i];
}
return min_counter_ballons;
}