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.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
temp.f95:34:32: 34 | function is_vowel_substring(word) result(res) | 1 Error: Procedure ‘is_vowel_substring’ at (1) has an explicit interface from a previous declaration temp.f95:35:21: 35 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:36:44: 36 | character(len=*), intent(in) :: word | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:37:22: 37 | logical :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:39:21: 39 | res = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:40:19: 40 | do i = 1, 5 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:41:54: 41 | if (index(word, ['a', 'e', 'i', 'o', 'u'][i]) == 0) exit | 1 Error: Syntax error in argument list at (1) temp.f95:42:11: 42 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:43:24: 43 | if (i == 6) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:44:24: 44 | res = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:45:11: 45 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:46:7: 46 | end function is_vowel_substring | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:28:16: 28 | if (is_vowel_substring(word(i:i+4))) then | 1 Error: ‘is_vowel_substring’ at (1) is not a function
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) == '
temp.f95:42:66: 42 | word(i:j) == 'iouauaeiouae' .or. word(i:j) == ' | 1 Error: Unterminated character constant beginning at (1) f951: Error: Unexpected end of file in ‘temp.f95’
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
.
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
.