Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b3c15e9

Browse filesBrowse files
author
Kohei Asai
authored
1220. Count Vowels Permutation (#126)
1 parent 3280605 commit b3c15e9
Copy full SHA for b3c15e9

File tree

2 files changed

+47
-0
lines changed
Filter options

2 files changed

+47
-0
lines changed

‎solutions/count_vowels_permutation.ts

Copy file name to clipboard
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 1220. Count Vowels Permutation
2+
// https://leetcode.com/problems/count-vowels-permutation/
3+
export default function countVowelPermutation(n: number): number {
4+
function traverse(nextChars: string[], n: number): number {
5+
if (n === 0) return 1;
6+
7+
let permutation = 0;
8+
9+
for (const char of nextChars) {
10+
const key = `${char}*${n}`;
11+
12+
if (!memo.has(key)) {
13+
memo.set(key, traverse(NEXT_CHARS_EACH_CHAR.get(char), n - 1));
14+
}
15+
16+
permutation += memo.get(key);
17+
}
18+
19+
return permutation % DIVISOR;
20+
}
21+
22+
return traverse(["a", "e", "i", "o", "u"], n);
23+
}
24+
25+
const memo = new Map();
26+
27+
const DIVISOR = 1e9 + 7;
28+
29+
const NEXT_CHARS_EACH_CHAR = new Map([
30+
["a", ["e"]],
31+
["e", ["a", "i"]],
32+
["i", ["a", "e", "o", "u"]],
33+
["o", ["i", "u"]],
34+
["u", ["a"]]
35+
]);
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { test } from "https://deno.land/std/testing/mod.ts";
2+
import { assertStrictEq } from "https://deno.land/std/testing/asserts.ts";
3+
import countVowelPermutation from "./count_vowels_permutation.ts";
4+
5+
test("1220. Count Vowels Permutation", () => {
6+
assertStrictEq(countVowelPermutation(1), 5);
7+
assertStrictEq(countVowelPermutation(2), 10);
8+
assertStrictEq(countVowelPermutation(5), 68);
9+
assertStrictEq(countVowelPermutation(144), 18208803);
10+
assertStrictEq(countVowelPermutation(2000), 793084836);
11+
assertStrictEq(countVowelPermutation(0), 1);
12+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.