You are given a 0-indexed array of string words
and two integers left
and right
.
A string is called a vowel string if it starts with a vowel character and ends with a vowel character where vowel characters are 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
.
Return the number of vowel strings words[i]
where i
belongs to the inclusive range [left, right]
.
Example 1:
Input: words = [ "are ", "amy ", "u "], left = 0, right = 2 Output: 2 Explanation: - "are " is a vowel string because it starts with 'a' and ends with 'e'. - "amy " is not a vowel string because it does not end with a vowel. - "u " is a vowel string because it starts with 'u' and ends with 'u'. The number of vowel strings in the mentioned range is 2.
Example 2:
Input: words = [ "hey ", "aeo ", "mu ", "ooo ", "artro "], left = 1, right = 4 Output: 3 Explanation: - "aeo " is a vowel string because it starts with 'a' and ends with 'o'. - "mu " is not a vowel string because it does not start with a vowel. - "ooo " is a vowel string because it starts with 'o' and ends with 'o'. - "artro " is a vowel string because it starts with 'a' and ends with 'o'. The number of vowel strings in the mentioned range is 3.
Constraints:
1 <= words.length <= 1000
1 <= words[i].length <= 10
words[i]
consists of only lowercase English letters.0 <= left <= right < words.length
program main
implicit none
integer :: i, j, n
character(len=10) :: words(1000)
logical :: vowel
! read input
read(*,*) n
do i = 1, n
read(*,*) words(i)
end do
read(*,*) i, j
! solve problem
vowel = .false.
do while (i <= j)
if (words(i)(1:1) == 'a' .or. &
words(i)(1:1) == 'e' .or. &
words(i)(1:1) == 'i' .or. &
words(i)(1:1) == 'o' .or. &
words(i)(1:1) == 'u') then
vowel = .true.
else
vowel = .false.
exit
end if
if (words(i)(len_trim(words(i)):len_trim(words(i))) == &
words(i)(1:1)) then
vowel = .true.
else
vowel = .false.
exit
end if
i = i + 1
end do
! print output
write(*,*) vowel
end program main
At line 9 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x78d619485960 in ??? #1 0x78d6194864d9 in ??? #2 0x78d6196da17b in ??? #3 0x78d6196d3684 in ??? #4 0x78d6196d42aa in ??? #5 0x5b5f2463222a in MAIN__ #6 0x5b5f246325f9 in main
module vowel_strings
implicit none
contains
integer function count_vowel_strings(words, left, right) result(count)
! This function returns the number of vowel strings in the given array of strings.
! A string is considered a vowel string if it starts and ends with a vowel.
! Parameters
integer, intent(in) :: left, right
character(len=*), intent(in) :: words(:)
! Variables
integer :: i, j
logical :: is_vowel
! Initialize the count to 0
count = 0
! Iterate over the words in the range [left, right]
do i = left, right
! Check if the word starts and ends with a vowel
is_vowel = .false.
do j = 1, len(words(i))
if (words(i)(j:j) == 'a' .or. words(i)(j:j) == 'e' .or. &
words(i)(j:j) == 'i' .or. words(i)(j:j) == 'o' .or. &
words(i)(j:j) == 'u') then
is_vowel = .true.
else
exit
end if
end do
! If the word is a vowel string, increment the count
if (is_vowel) then
count = count + 1
end if
end do
end function count_vowel_strings
end module vowel_strings
! Test the function with some examples
program test_vowel_strings
use vowel_strings
implicit none
! Examples from the problem statement
print *, count_vowel_strings(["are ", "amy ", "u "], 0, 2)
print *, count_vowel_strings(["hey ", "aeo ", "mu ", "ooo ", "artro "], 1, 4)
! Additional examples
print *, count_vowel_strings(["a", "e", "i", "o", "u"], 0, 4)
print *, count_vowel_strings(["a", "e", "i", "o", "u"], 1, 3)
print *, count_vowel_strings(["a", "e", "i", "o", "u"], 2, 2)
end program test_vowel_strings
temp.f95:57:45: 57 | print *, count_vowel_strings(["are ", "amy ", "u "], 0, 2) | 1 Error: Different CHARACTER lengths (4/2) in array constructor at (1) temp.f95:58:45: 58 | print *, count_vowel_strings(["hey ", "aeo ", "mu ", "ooo ", "artro "], 1, 4) | 1 Error: Different CHARACTER lengths (4/3) in array constructor at (1)
def is_vowel(c):
return c in 'aeiou'
def count_vowel_strings(words, left, right):
count = 0
for i in range(left, right + 1):
if is_vowel(words[i][0]) and is_vowel(words[i][-1]):
count += 1
return count
We define is_vowel
function that checks whether a given character is a vowel or not. In our count_vowel_strings
function, we iterate over the range [left, right]
and check if the first and last characters of the word at position i
are vowels. If both are vowels, we increment the count
. Finally, we return count
as the result.
#include <string>
#include <vector>
bool is_vowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
int count_vowel_strings(std::vector<std::string>& words, int left, int right) {
int count = 0;
for (int i = left; i <= right; i++) {
if (is_vowel(words[i][0]) && is_vowel(words[i][words[i].length() - 1])) {
count++;
}
}
return count;
}
We define is_vowel
function that checks whether a given character is a vowel or not. In our count_vowel_strings
function, we iterate over the range [left, right]
and check if the first and last characters of the word at position i
are vowels. If both are vowels, we increment the count
. Finally, we return count
as the result.