Count the Number of Vowel Strings in Range

🏠 ⬅️ ➡️

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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.

🌐 Data from online sources
#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.