Count redundant substrings by vowel and consonant weights
Write a function that counts the redundant substrings of a word.
A string s is redundant when its length equals a * V + b * C, where V is the number of vowels in s, C is the number of consonants in s, and a and b are given integers. The vowels are a, e, i, o, u; every other letter (including y) is a consonant.
def count_redundant_substrings(word: str, a: int, b: int) -> int: ...
Return how many of the n(n+1)/2 non-empty contiguous substrings of word are redundant. Substrings at different positions count separately, even if their text is the same.
Constraints
-
0 <= len(word) <= 10^5, lowercase English letters only. -
-10^5 <= a, b <= 10^5. - The answer can exceed 2^31.
Example 1
word = "code", a = 2, b = 0 -> 4
The rule becomes V + C = 2V, so V == C. The redundant substrings are "co", "od", "de" and "code".
Example 2
word = "queue", a = 1, b = 0 -> 10
The rule becomes V + C = V, so C == 0. Only substrings made entirely of vowels count, and "ueue" has 10 of them.
An O(n^2) solution is too slow for the largest inputs; aim for O(n).
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
