How to count all substrings in a string, and then sort based on if they begin with a vowel

Question:

I tried doing it like this:

# the system waits for the user's string input
word = input() 

# defining vowels
vowels = "aeiou"

#// BEGIN_TODO [count_substrings] counting all substrings in a string
count_c: int = 0
count_v: int = 0
total_substrings = (len(word)*(len(word)+1)) // 2
for letter in word:
    if letter in vowels:
        for i in range(total_substrings):
            for j in range(i, total_substrings):
                count_v += 1
    if letter not in vowels:
        for i in range(total_substrings):
            for j in range(i, total_substrings):
                count_c += 1
print('number of substrings starting with Vowels: {} - number of substrings starting with Consonants: {}'.format(count_v, count_c))
#// END_TODO [count_substrings]

but the code outputs strange numbers. Help would be appreciated on how to approach this problem.

Asked By: Nasrr

||

Answers:

Looks like you’re trying to do the same thing twice: You use a formula to calculate the number of substrings, but then you also try to count them with loops. One of those is unnecessary. Or you could combine a little calculation and a little counting like this:

VOWELS = "aeiou"
word = 'test'

n = len(word)
count_v: int = 0
count_c: int = 0

for i, letter in enumerate(word):
    # number of substrings starting at i, not counting the empty string:
    n_substrings = n - i
    if letter in VOWELS:
        count_v += n_substrings
    else:
        count_c += n_substrings
        
print(f"number of substrings starting with vowels: {count_v}")
print(f"number of substrings starting with consonants: {count_c}")
number of substrings starting with vowels: 3
number of substrings starting with consonants: 7

Note, however, that this approach will overcount if there are identical substrings that can be started at different points in the string. For example, if word = 'eel', the substring 'e' would be counted twice. If you don’t want that, it gets more complicated. You could extract all the substrings and collect them in two sets (one for those starting with a vowel, and one for the rest), to remove the duplicates.

Answered By: Arne
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.