Maximum Number of Balloons

🏠 ⬅️ ➡️

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 problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i, j, k, n, m
    character(len=100) :: text
    character(len=7), parameter :: balloon = "balloon "
    integer, allocatable :: dp(:,:)
    integer :: max_instances

    ! read input
    read(*,'(A)') text

    ! allocate dynamic memory for dp array
    n = len(text)
    allocate(dp(n+1,n))

    ! initialize dp array
    do i = 1, n+1
        dp(i,1) = 1
    end do
    do i = 2, n
        do j = 2, n
            if (text(j-1:j-1) == balloon(i-1:i-1)) then
                dp(j,i) = dp(j-1,i-1) + 1
            else
                dp(j,i) = max(dp(j-1,i), dp(j,i-1))
            end if
        end do
    end do

    ! find maximum number of instances
    max_instances = 0
    do i = 1, n
        do j = 1, n
            if (dp(j,i) > max_instances) then
                max_instances = dp(j,i)
            end if
        end do
    end do

    ! print result
    write(*,*) max_instances

    ! deallocate dynamic memory
    deallocate(dp)

end program main
Compiled
Executed
Correct
! Declare variables
integer :: i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
character(len=100) :: text
integer :: max_instances

! Read input
read(*,*) text

! Initialize variables
max_instances = 0

! Loop through each character in the text
do i = 1, len_trim(text)
    ! Check if the current character is 'b'
    if (text(i:i) == 'b') then
        ! Loop through each character in the text starting from the next character
        do j = i + 1, len_trim(text)
            ! Check if the current character is 'a'
            if (text(j:j) == 'a') then
                ! Loop through each character in the text starting from the next character
                do k = j + 1, len_trim(text)
                    ! Check if the current character is 'l'
                    if (text(k:k) == 'l') then
                        ! Loop through each character in the text starting from the next character
                        do l = k + 1, len_trim(text)
                            ! Check if the current character is 'o'
                            if (text(l:l) == 'o') then
                                ! Loop through each character in the text starting from the next character
                                do m = l + 1, len_trim(text)
                                    ! Check if the current character is 'n'
                                    if (text(m:m) == 'n') then
                                        ! Loop through each character in the text starting from the next character
                                        do n = m + 1, len_trim(text)
                                            ! Check if the current character is ' '
                                            if (text(n:n) == ' ') then
                                                ! Increment the number of instances
                                                max_instances = max_instances + 1
                                            end if
                                        end do
                                    end if
                                end do
                            end if
                        end do
                    end if
                end do
            end if
        end do
    end if
end do

! Output the maximum number of instances
write(*,*) max_instances

end
🌐 Data from online sources
def max_number_of_balloons(text: str) -> int:
    letter_count = [0] * 5
    for c in text:
        if c == 'b': letter_count[0] += 1
        if c == 'a': letter_count[1] += 1
        if c == 'l': letter_count[2] += 1
        if c == 'o': letter_count[3] += 1
        if c == 'n': letter_count[4] += 1
    letter_count[2] //= 2
    letter_count[3] //= 2
    return min(letter_count)
  1. First, we initialize an array letter_count with a length of 5 to represent each character in the word "balloon".
  2. We then loop through each character in the input string text.
  3. For each character, we increment the corresponding index in the letter_count array if it matches one of the characters in the word "balloon".
  4. After the loop, we divide the count of 'l' and 'o' by 2 since they appear twice in the word "balloon".
  5. Finally, we return the minimum value in the letter_count array, which represents the maximum number of instances of the word "balloon" that can be formed.
🌐 Data from online sources
int maxNumberOfBalloons(string text) {
    int letter_count[5] = {0};
    for (char c : text) {
        if (c == 'b') letter_count[0]++;
        if (c == 'a') letter_count[1]++;
        if (c == 'l') letter_count[2]++;
        if (c == 'o') letter_count[3]++;
        if (c == 'n') letter_count[4]++;
    }
    letter_count[2] /= 2;
    letter_count[3] /= 2;
    return *min_element(letter_count, letter_count + 5);
}
  1. First, we initialize an array letter_count with a length of 5 to represent each character in the word "balloon".
  2. We then loop through each character in the input string text.
  3. For each character, we increment the corresponding index in the letter_count array if it matches one of the characters in the word "balloon".
  4. After the loop, we divide the count of 'l' and 'o' by 2 since they appear twice in the word "balloon".
  5. Finally, we return the minimum value in the letter_count array, which represents the maximum number of instances of the word "balloon" that can be formed.