# LeetCode: Count Vowels Permutation Solution

Memoized recursion

## Approach

recursion(i, char)
number of valid strings of
n - i
length, starting with
char

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * @param {number} n3 * @return {number}4 */5var countVowelPermutation = function (n) {6  const MOD = 1e9 + 77
8  const possibleNext = {9    a: "e",10    e: "ai",11    i: "aeou",12    o: "iu",13    u: "a",14  }15
16  const memo = Array.from({ length: n }, _ => ({}))17
18  const recursion = (i, char, possibleChars = "aeiou") => {19    if (i === n) return 120
21    if (memo[i][char]) return memo[i][char]22
23    let res = 024
25    for (const possibleChar of possibleChars) {26      res =27        (res + recursion(i + 1, possibleChar, possibleNext[possibleChar])) % MOD28    }29
30    return (memo[i][char] = res)31  }32
33  return recursion(0)34}```

## Tags

leetcode

dynamic programming

## Next Post

LeetCode: Reshape the Matrix

Jul 5, 2021

Flat, reverse, and pop

## Previous Post

LeetCode: Find K Closest Elements

Jul 3, 2021

Sort with custom comparator

Search Posts