TheAlgorithms-C/leetcode/src/1189.c

38 lines
1004 B
C
Raw Normal View History

2019-10-03 10:00:27 +08:00
int maxNumberOfBalloons(char * text){
/*
0 -> b,
1 -> a,
2 -> l,
2019-10-04 07:14:15 +08:00
3 -> o,
4 -> n
2019-10-03 10:00:27 +08:00
*/
2019-10-04 07:14:15 +08:00
int count_letters[5] = {0};
int i, min_counter_ballons;
2019-10-03 10:00:27 +08:00
2019-10-04 07:14:15 +08:00
for (char *ptr = text; *ptr; ptr++) {
if(*ptr == 'b') {
2019-10-03 10:00:27 +08:00
count_letters[0]++;
2019-10-04 07:14:15 +08:00
}else if(*ptr == 'a') {
2019-10-03 10:00:27 +08:00
count_letters[1]++;
2019-10-04 07:14:15 +08:00
}else if(*ptr == 'l') {
2019-10-03 10:00:27 +08:00
count_letters[2]++;
2019-10-04 07:14:15 +08:00
}else if(*ptr == 'o') {
2019-10-03 10:00:27 +08:00
count_letters[3]++;
2019-10-04 07:14:15 +08:00
}else if(*ptr == 'n') {
2019-10-03 10:00:27 +08:00
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 */
2019-10-04 07:14:15 +08:00
min_counter_ballons = count_letters[0];
for(i = 1; i < 5; i++){
if(count_letters[i] < min_counter_ballons)
2019-10-03 10:00:27 +08:00
min_counter_ballons = count_letters[i];
}
return min_counter_ballons;
2019-10-04 07:14:15 +08:00
}