Snowflake OA Interview Problem: String Patterns | Dynamic Programming Counting

20 Views
No Comments

String Patterns

Given the length of a word wordLen and the maximum number of consecutive vowels it can contain maxVowels, determine how many unique words can be generated.

Words will consist of English alphabetic letters a through z only.

Vowels are {a, e, i, o, u}; consonants are the remaining 21 letters.

In the explanations, v and c represent vowels and consonants.

Return the number of possible words modulo 10^9 + 7.

Example

wordLen = 1
maxVowels = 1

There are 26 possibilities, one for each letter in the alphabet.

wordLen = 4
maxVowels = 1

Valid patterns include cccc, vccc, cvcc, ccvc, cccv, and vcvc.

Return the count modulo 10^9 + 7.

This problem counts how many strings of length wordLen can be formed using 26 letters while never exceeding maxVowels consecutive vowels. The standard solution is dynamic programming or DFS with memoization, where the state tracks the current position and the number of consecutive vowels at the end. From each state, we either place a consonant (21 choices, reset the vowel streak) or a vowel (5 choices, increase the streak if still within the limit). The result is computed modulo 10^9 + 7.

END
 0