aboutsummaryrefslogtreecommitdiff
path: root/vowels_substrings.py
blob: 0993983b66c4311cfeef505e5669d97805732fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 2063. Vowels of All Substrings

"""
given a string 'word, return thesum of the number of vowels (a, e, i, o, u)
in every substring of 'word'. a substringis a contiguous (non-empty) sequence
of characters within a string. note, due to the large constraints, the answer
may not fit in a signed 32-bit integer. please be careful during
calculates...
"""


class Solution(object):
    def countVowels(self, word):
        ans, n = 0, len(word)
        d = {"a": 1, "e": 1, "i": 1, "o": 1, "u": 1}
        for i in range(n):
            if word[i] in d:
                ans += (n - i) * (i + 1)
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.countVowels("aba"))  # expect: 6
    print(obj.countVowels("abc"))  # expect: 3
    print(obj.countVowels("ltcd"))  # expect: 0