Problem · String

Get Redundant Substrings

Learn this problem
HardAmazonFULLTIMEOA
See Amazon hiring insights

Problem statement

Data analysts at Amazon are building a utility to identify redundant words in advertisements.

They define a string as redundant if the length of the string, |word| = a * V + b * C, where a and b are given integers and V and C are the numbers of vowels and consonants in the string. Note that for any substring, |word| = V + C, so a substring is redundant when V + C = a * V + b * C.

Given a string word, and two integers a and b, find the number of redundant substrings of word.

A substring is a contiguous, non-empty group of characters in a string. For example, "bcb" is a substring of "abcba", while "bba" is not. The empty substring is not counted.

Vowels are the letters a, e, i, o, u; all other lowercase English letters are consonants.

Function

getRedundantSubstrings(word: String, a: int, b: int) → int

Complete the function getRedundantSubstrings in the editor below.

Returns

int: the number of redundant substrings

Examples

Example 1

word = "abbacc"a = -1b = 2return = 5
The condition |word| = a*V + b*C with a=-1, b=2 reduces to C = 2V. The non-empty substrings of "abbacc" satisfying this are "abb" (V=1, C=2), "bba" (V=1, C=2), "bac" (V=1, C=2), "acc" (V=1, C=2), and "abbacc" (V=2, C=4), giving 5 redundant substrings.

Example 2

word = "akljfs"a = -2b = 1return = 15
The condition |word| = a*V + b*C with a=-2, b=1 reduces to V = 0, i.e. substrings containing no vowels. In "akljfs" only the index-0 character 'a' is a vowel, so the vowel-free substrings are exactly the non-empty substrings of "kljfs" (length 5), of which there are 5*6/2 = 15.

Constraints

  • 1 ≤ |word| ≤ 10^5
  • -10^3 ≤ a ≤ 10^3
  • -10^3 ≤ b ≤ 10^3
  • word contains lowercase English letters, [a-z].

More Amazon problems

drafts saved locally
public int getRedundantSubstrings(String word, int a, int b) {
  // write your code here
}
word"abbacc"
a-1
b2
expected5
checking account