LeetCode: Count Vowels Permutation Solution
Memoized recursionApproach
recursion(i, char)
number of valid strings of n - i
length, starting with char
Implementation
1/**2 * @param {number} n3 * @return {number}4 */5var countVowelPermutation = function (n) {6 const MOD = 1e9 + 778 const possibleNext = {9 a: "e",10 e: "ai",11 i: "aeou",12 o: "iu",13 u: "a",14 }1516 const memo = Array.from({ length: n }, _ => ({}))1718 const recursion = (i, char, possibleChars = "aeiou") => {19 if (i === n) return 12021 if (memo[i][char]) return memo[i][char]2223 let res = 02425 for (const possibleChar of possibleChars) {26 res =27 (res + recursion(i + 1, possibleChar, possibleNext[possibleChar])) % MOD28 }2930 return (memo[i][char] = res)31 }3233 return recursion(0)34}
Comments
Loading comments...
Tags
leetcode
dynamic programming
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.