Skip to content

Commit

Permalink
Add 1189 in c language
Browse files Browse the repository at this point in the history
  • Loading branch information
julienChemillier authored Oct 27, 2022
1 parent 98b6666 commit 21c764b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions c/1189-Maximum-Number-of-Balloons.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
Space: O(1)
Time: O(n)
*/


int min(int a, int b) {
return a<b?a:b;
}

int maxNumberOfBalloons(char * text){
// Counter for each letter
int b=0;
int a=0;
int l=0;
int o=0;
int n=0;
for (int i=0; text[i]!='\0'; i++) {
if (text[i]=='b') {
b++;
} else if (text[i]=='a') {
a++;
} else if (text[i]=='l') {
l++;
} else if (text[i]=='o') {
o++;
} else if (text[i]=='n') {
n++;
}
}
l /= 2; // There is 2 'l' in balloon
o /= 2; // There is 2 'o' in balloon
return min(b, min(a, min(l, min(o, n))));
}

0 comments on commit 21c764b

Please sign in to comment.