diff options
Diffstat (limited to 'vowels_substrings.py')
-rw-r--r-- | vowels_substrings.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/vowels_substrings.py b/vowels_substrings.py new file mode 100644 index 0000000..0993983 --- /dev/null +++ b/vowels_substrings.py @@ -0,0 +1,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 |