Count Vowel Substrings of a String

🏠 ⬅️ ➡️

A substring is a contiguous (non-empty) sequence of characters within a string.

A vowel substring is a substring that only consists of vowels ('a', 'e', 'i', 'o', and 'u') and has all five vowels present in it.

Given a string word, return the number of vowel substrings in word.

Example 1:

Input: word = "aeiouu " Output: 2 Explanation: The vowel substrings of word are as follows (underlined): - "aeiouu " - "aeiouu "

Example 2:

Input: word = "unicornarihan " Output: 0 Explanation: Not all 5 vowels are present, so there are no vowel substrings.

Example 3:

Input: word = "cuaieuouac " Output: 7 Explanation: The vowel substrings of word are as follows (underlined): - "cuaieuouac " - "cuaieuouac " - "cuaieuouac " - "cuaieuouac " - "cuaieuouac " - "cuaieuouac " - "cuaieuouac "

Constraints:

  • 1 <= word.length <= 100
  • word consists of lowercase English letters only.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i, j, n
    character(len=100) :: word
    logical :: is_vowel_substring

    ! Example 1
    word = 'aeiouu '
    print '(I0)', count_vowel_substrings(word)

    ! Example 2
    word = 'unicornarihan '
    print '(I0)', count_vowel_substrings(word)

    ! Example 3
    word = 'cuaieuouac '
    print '(I0)', count_vowel_substrings(word)

contains

    function count_vowel_substrings(word) result(n)
        implicit none
        character(len=*), intent(in) :: word
        integer :: n

        do i = 1, len(word) - 4
            if (is_vowel_substring(word(i:i+4))) then
                n = n + 1
            end if
        end do
    end function count_vowel_substrings

    function is_vowel_substring(word) result(res)
        implicit none
        character(len=*), intent(in) :: word
        logical :: res

        res = .false.
        do i = 1, 5
            if (index(word, ['a', 'e', 'i', 'o', 'u'][i]) == 0) exit
        end do
        if (i == 6) then
            res = .true.
        end if
    end function is_vowel_substring
end program main
Compiled
Executed
Correct
program vowel_substrings
      implicit none

      integer :: i, j, n, count
      character(len=100) :: word

      ! Read the input word
      read (*,*) word

      ! Initialize the count
      count = 0

      ! Loop through the characters of the word
      do i = 1, len_trim(word)
         ! Check if the current character is a vowel
         if (word(i:i) == 'a' .or. word(i:i) == 'e' .or. &
             word(i:i) == 'i' .or. word(i:i) == 'o' .or. &
             word(i:i) == 'u') then
            ! Loop through the remaining characters of the word
            do j = i + 1, len_trim(word)
               ! Check if the substring starting at i and ending at j is a vowel substring
               if (word(i:j) == 'aeiou' .or. word(i:j) == 'aeioua' .or. &
                   word(i:j) == 'aeiouae' .or. word(i:j) == 'aeiouao' .or. &
                   word(i:j) == 'aeiouau' .or. word(i:j) == 'aeiouei' .or. &
                   word(i:j) == 'aeiouio' .or. word(i:j) == 'aeiouiu' .or. &
                   word(i:j) == 'aeiouoa' .or. word(i:j) == 'aeiouou' .or. &
                   word(i:j) == 'eaiouae' .or. word(i:j) == 'eaiouao' .or. &
                   word(i:j) == 'eaiouau' .or. word(i:j) == 'eaiouei' .or. &
                   word(i:j) == 'eaiouio' .or. word(i:j) == 'eaiouiu' .or. &
                   word(i:j) == 'eaiouoa' .or. word(i:j) == 'eaiouou' .or. &
                   word(i:j) == 'iouaeiou' .or. word(i:j) == 'iouaeioua' .or. &
                   word(i:j) == 'iouaeiouae' .or. word(i:j) == 'iouaeiouao' .or. &
                   word(i:j) == 'iouaeiouau' .or. word(i:j) == 'iouaeiouei' .or. &
                   word(i:j) == 'iouaeiouio' .or. word(i:j) == 'iouaeiouiu' .or. &
                   word(i:j) == 'iouaeiouoa' .or. word(i:j) == 'iouaeiouou' .or. &
                   word(i:j) == 'iouaoaeiou' .or. word(i:j) == 'iouaoaeioua' .or. &
                   word(i:j) == 'iouaoaeiouae' .or. word(i:j) == 'iouaoaeiouao' .or. &
                   word(i:j) == 'iouaoaeiouau' .or. word(i:j) == 'iouaoaeiouei' .or. &
                   word(i:j) == 'iouaoaeiouio' .or. word(i:j) == 'iouaoaeiouiu' .or. &
                   word(i:j) == 'iouaoaeiouoa' .or. word(i:j) == 'iouaoaeiouou' .or. &
                   word(i:j) == 'iouauaeiou' .or. word(i:j) == 'iouauaeioua' .or. &
                   word(i:j) == 'iouauaeiouae' .or. word(i:j) == '
🌐 Data from online sources
def count_vowel_substrings(word: str) -> int:
    count, a, e, i, o, u = 0, 0, 0, 0, 0, 0
    for c in word:
        if c == 'a': a += 1
        elif c == 'e': e = a + e
        elif c == 'i': i = e + i
        elif c == 'o': o = i + o
        elif c == 'u': u = o + u
    return u

The algorithm counts the number of vowel substrings in the given input word. The basic idea is to maintain separate counter variables for each vowel a, e, i, o, and u. When iterating through the characters of the input string, the algorithm increases or updates the respective counter variables based on whether a character is a given type of vowel. The updated counter variables keep track of the number of vowel substrings. Once the iteration is completed, the result can be obtained from the counter variable of last vowel u. This solution has a time complexity of O(N), where N is the length of the input string word.

🌐 Data from online sources
int countVowelSubstrings(string word) {
    int count = 0, a = 0, e = 0, i = 0, o = 0, u = 0;
    for (char c : word) {
        if (c == 'a') a++;
        else if (c == 'e') e = a + e;
        else if (c == 'i') i = e + i;
        else if (c == 'o') o = i + o;
        else if (c == 'u') u = o + u;
    }
    return u;
}

The algorithm counts the number of vowel substrings in the given input word. The basic idea is to maintain separate counter variables for each vowel a, e, i, o, and u. When iterating through the characters of the input string, the algorithm increases or updates the respective counter variables based on whether a character is a given type of vowel. The updated counter variables keep track of the number of vowel substrings. Once the iteration is completed, the result can be obtained from the counter variable of last vowel u. This solution has a time complexity of O(N), where N is the length of the input string word.