Hash Table
String
Counting
Description
Given a string text
, you want to use the characters of text
to form as many instances of the word "balloon" as possible.
You can use each character in text
at most once . Return the maximum number of instances that can be formed.
Example 1:
Input: text = "nlaebolko"
Output: 1
Example 2:
Input: text = "loonbalxballpoon"
Output: 2
Example 3:
Input: text = "leetcode"
Output: 0
Constraints:
1 <= text.length <= 104
text
consists of lower case English letters only.
Note: This question is the same as 2287: Rearrange Characters to Make Target String.
Solutions
Solution 1: Counting
We count the frequency of each letter in the string text
, and then divide the frequency of the letters 'o' and 'l' by 2, because the word balloon
contains the letters 'o' and 'l' twice.
Next, we traverse each letter in the word balon
, and find the minimum frequency of each letter in the string text
. This minimum frequency is the maximum number of times the word balloon
can appear in the string text
.
The time complexity is $O(n)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string text
, and $C$ is the size of the character set. In this problem, $C = 26$.
Python3 Java C++ Go TypeScript Rust PHP
class Solution :
def maxNumberOfBalloons ( self , text : str ) -> int :
cnt = Counter ( text )
cnt [ 'o' ] >>= 1
cnt [ 'l' ] >>= 1
return min ( cnt [ c ] for c in 'balon' )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 class Solution {
public int maxNumberOfBalloons ( String text ) {
int [] cnt = new int [ 26 ] ;
for ( int i = 0 ; i < text . length (); ++ i ) {
++ cnt [ text . charAt ( i ) - 'a' ] ;
}
cnt [ 'l' - 'a' ] >>= 1 ;
cnt [ 'o' - 'a' ] >>= 1 ;
int ans = 1 << 30 ;
for ( char c : "balon" . toCharArray ()) {
ans = Math . min ( ans , cnt [ c - 'a' ] );
}
return ans ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 class Solution {
public :
int maxNumberOfBalloons ( string text ) {
int cnt [ 26 ]{};
for ( char c : text ) {
++ cnt [ c - 'a' ];
}
cnt [ 'o' - 'a' ] >>= 1 ;
cnt [ 'l' - 'a' ] >>= 1 ;
int ans = 1 << 30 ;
string t = "balon" ;
for ( char c : t ) {
ans = min ( ans , cnt [ c - 'a' ]);
}
return ans ;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 func maxNumberOfBalloons ( text string ) int {
cnt := [ 26 ] int {}
for _ , c := range text {
cnt [ c - 'a' ] ++
}
cnt [ 'l' - 'a' ] >>= 1
cnt [ 'o' - 'a' ] >>= 1
ans := 1 << 30
for _ , c := range "balon" {
if x := cnt [ c - 'a' ]; ans > x {
ans = x
}
}
return ans
}
function maxNumberOfBalloons ( text : string ) : number {
const cnt = new Array ( 26 ). fill ( 0 );
for ( const c of text ) {
cnt [ c . charCodeAt ( 0 ) - 97 ] ++ ;
}
return Math . min ( cnt [ 0 ], cnt [ 1 ], cnt [ 11 ] >> 1 , cnt [ 14 ] >> 1 , cnt [ 13 ]);
}
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 impl Solution {
pub fn max_number_of_balloons ( text : String ) -> i32 {
let mut arr = [ 0 ; 5 ];
for c in text . chars () {
match c {
'b' => {
arr [ 0 ] += 1 ;
}
'a' => {
arr [ 1 ] += 1 ;
}
'l' => {
arr [ 2 ] += 1 ;
}
'o' => {
arr [ 3 ] += 1 ;
}
'n' => {
arr [ 4 ] += 1 ;
}
_ => {}
}
}
arr [ 2 ] /= 2 ;
arr [ 3 ] /= 2 ;
let mut res = i32 :: MAX ;
for num in arr {
res = res . min ( num );
}
res
}
}
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 class Solution {
/**
* @param String $text
* @return Integer
*/
function maxNumberOfBalloons($text) {
$cnt1 = $cnt2 = $cnt3 = $cnt4 = $cnt5 = 0;
for ($i = 0; $i < strlen($text); $i++) {
if ($text[$i] == 'b') {
$cnt1 += 1;
} elseif ($text[$i] == 'a') {
$cnt2 += 1;
} elseif ($text[$i] == 'l') {
$cnt3 += 1;
} elseif ($text[$i] == 'o') {
$cnt4 += 1;
} elseif ($text[$i] == 'n') {
$cnt5 += 1;
}
}
$cnt3 = floor($cnt3 / 2);
$cnt4 = floor($cnt4 / 2);
return min($cnt1, $cnt2, $cnt3, $cnt4, $cnt5);
}
}