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.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
At line 11 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7bce65508960 in ??? #1 0x7bce655094d9 in ??? #2 0x7bce6575d17b in ??? #3 0x7bce6575d752 in ??? #4 0x7bce65759f1b in ??? #5 0x7bce6575ee3c in ??? #6 0x7bce6575fe55 in ??? #7 0x5d398932f2a5 in MAIN__ #8 0x5d398932f7ee in main
! 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
At line 7 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x795d6ee1c960 in ??? #1 0x795d6ee1d4d9 in ??? #2 0x795d6f07117b in ??? #3 0x795d6f06a684 in ??? #4 0x795d6f06b2aa in ??? #5 0x59591ae02225 in MAIN__ #6 0x59591ae024c9 in main
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)
letter_count
with a length of 5 to represent each character in the word "balloon".text
.letter_count
array if it matches one of the characters in the word "balloon".letter_count
array, which represents the maximum number of instances of the word "balloon" that can be formed.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);
}
letter_count
with a length of 5 to represent each character in the word "balloon".text
.letter_count
array if it matches one of the characters in the word "balloon".letter_count
array, which represents the maximum number of instances of the word "balloon" that can be formed.