Balloon Everywhere
Last Updated :
05 Apr, 2023
Bob is very fond of balloons. Once he visited an amusement park with his mother. The mother told Bob that she would buy him a balloon only if he answer her problem right. She gave Bob a string S [contains only lowercase characters] and asked him to use the characters of the string to form as many instances of the word “balloon” as possible. Return the maximum number of instances that can be formed.
Help Bob to solve the problem.
Note: You can use each character in the string at most once.
Examples:
Input: S = “nlaebolko”
Output: 1
Explanation: Here, the number of occurrences of ‘b’ = 1, the occurrence of ‘a’ = 1, the occurrence of ‘l’ = 2, the occurrence of ‘o’ = 2, the occurrence of ‘n’ = 1. So, we can form 1 “balloon” using the letters.
Input: S = “loonbalxballpoon”
Output: 2
Explanation: Here, the number of occurrence of ‘b’ = 2, occurrence of ‘a’ = 2, occurrence of ‘l’ = 4, occurrence of ‘o’ = 4, occurrence of ‘n’ = 2. So, we can form 2 “balloon” using the letters.
Approach: To solve the problem follow the below idea and steps:
So, to form “balloon” 1 time we need at least 1->b, 1->a, 2->1, 2->o, 1->n.
- So, for checking this we have to store the frequency of elements of string S.
- We do so by taking an array of size 26 as there are total 26 characters.
- After that, we run a loop to check how many times we can make a balloon and if at any point we won’t be able to make it then we will return our answer.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxInstance(string S)
{
int arr[26] = { 0 };
for ( char x : S)
arr[x - 'a' ]++;
int ans = 0;
while ( true ) {
if (arr[ 'b' - 'a' ] and arr[ 'a' - 'a' ]
and (arr[ 'l' - 'a' ] > 1)
and (arr[ 'o' - 'a' ] > 1) and arr[ 'n' - 'a' ]) {
ans++;
arr[ 'b' - 'a' ]--;
arr[ 'a' - 'a' ]--;
arr[ 'l' - 'a' ] -= 2;
arr[ 'o' - 'a' ] -= 2;
arr[ 'n' - 'a' ]--;
}
else
return ans;
}
}
int main()
{
string S = "nlaebolko" ;
cout << maxInstance(S) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxInstance(String S)
{
int [] arr = new int [ 26 ];
for ( char x : S.toCharArray())
arr[x - 'a' ]++;
int ans = 0 ;
while ( true ) {
if (arr[ 'b' - 'a' ] > 0 && arr[ 'a' - 'a' ] > 0
&& arr[ 'l' - 'a' ] > 1 && arr[ 'o' - 'a' ] > 1
&& arr[ 'n' - 'a' ] > 0 ) {
ans++;
arr[ 'b' - 'a' ]--;
arr[ 'a' - 'a' ]--;
arr[ 'l' - 'a' ] -= 2 ;
arr[ 'o' - 'a' ] -= 2 ;
arr[ 'n' - 'a' ]--;
}
else {
return ans;
}
}
}
public static void main(String[] args)
{
String S = "nlaebolko" ;
System.out.println(maxInstance(S));
}
}
|
Python3
def max_instance(S):
arr = [ 0 ] * 26
for x in S:
arr[ ord (x) - ord ( 'a' )] + = 1
ans = 0
while True :
if arr[ ord ( 'b' ) - ord ( 'a' )] and arr[ ord ( 'a' ) - ord ( 'a' )] and (arr[ ord ( 'l' ) - ord ( 'a' )] > 1 ) and (arr[ ord ( 'o' ) - ord ( 'a' )] > 1 ) and arr[ ord ( 'n' ) - ord ( 'a' )]:
ans + = 1
arr[ ord ( 'b' ) - ord ( 'a' )] - = 1
arr[ ord ( 'a' ) - ord ( 'a' )] - = 1
arr[ ord ( 'l' ) - ord ( 'a' )] - = 2
arr[ ord ( 'o' ) - ord ( 'a' )] - = 2
arr[ ord ( 'n' ) - ord ( 'a' )] - = 1
else :
return ans
if __name__ = = '__main__' :
S = "nlaebolko"
print (max_instance(S))
|
Javascript
function maxInstance(S) {
const arr = new Array(26).fill(0);
for (const x of S) {
arr[x.charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
let ans = 0;
while ( true ) {
if (arr[ 'b' .charCodeAt(0) - 'a' .charCodeAt(0)] &&
arr[ 'a' .charCodeAt(0) - 'a' .charCodeAt(0)] &&
(arr[ 'l' .charCodeAt(0) - 'a' .charCodeAt(0)] > 1) &&
(arr[ 'o' .charCodeAt(0) - 'a' .charCodeAt(0)] > 1) &&
arr[ 'n' .charCodeAt(0) - 'a' .charCodeAt(0)])
{
ans++;
arr[ 'b' .charCodeAt(0) - 'a' .charCodeAt(0)]--;
arr[ 'a' .charCodeAt(0) - 'a' .charCodeAt(0)]--;
arr[ 'l' .charCodeAt(0) - 'a' .charCodeAt(0)] -= 2;
arr[ 'o' .charCodeAt(0) - 'a' .charCodeAt(0)] -= 2;
arr[ 'n' .charCodeAt(0) - 'a' .charCodeAt(0)]--;
} else {
return ans;
}
}
}
const S = "nlaebolko" ;
console.log(maxInstance(S));
|
C#
using System;
class GFG
{
static int maxInstance(String S)
{
int [] arr = new int [26];
foreach ( char x in S.ToCharArray())
arr[x - 'a' ]++;
int ans = 0;
while ( true )
{
if (arr[ 'b' - 'a' ] > 0 && arr[ 'a' - 'a' ] > 0
&& arr[ 'l' - 'a' ] > 1 && arr[ 'o' - 'a' ] > 1
&& arr[ 'n' - 'a' ] > 0)
{
ans++;
arr[ 'b' - 'a' ]--;
arr[ 'a' - 'a' ]--;
arr[ 'l' - 'a' ] -= 2;
arr[ 'o' - 'a' ] -= 2;
arr[ 'n' - 'a' ]--;
}
else
{
return ans;
}
}
}
public static void Main(String[] args)
{
String S = "nlaebolko" ;
Console.Write(maxInstance(S));
}
}
|
Time Complexity: O(N), where N is the length of S
Auxiliary Space: O(26).
Share your thoughts in the comments
Please Login to comment...