diff --git a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README.md b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README.md index 4c5f66ab3b634..254a012c014fd 100644 --- a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README.md +++ b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README.md @@ -159,6 +159,54 @@ public: }; ``` +#### TypeScript + +```ts +function getHappyString(n: number, k: number): string { + const ans: string[] = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} +``` + +#### JavaScript + +```js +function getHappyString(n, k) { + const ans = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} +``` + diff --git a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README_EN.md b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README_EN.md index 058a45cf2e8bf..f1c0ddf2c09dd 100644 --- a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README_EN.md +++ b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README_EN.md @@ -146,6 +146,54 @@ public: }; ``` +#### TypeScript + +```ts +function getHappyString(n: number, k: number): string { + const ans: string[] = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} +``` + +#### JavaScript + +```js +function getHappyString(n, k) { + const ans = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} +``` + diff --git a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.js b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.js new file mode 100644 index 0000000000000..e349343e318ff --- /dev/null +++ b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.js @@ -0,0 +1,19 @@ +function getHappyString(n, k) { + const ans = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} diff --git a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.ts b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.ts new file mode 100644 index 0000000000000..8f421ccaf92f4 --- /dev/null +++ b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.ts @@ -0,0 +1,19 @@ +function getHappyString(n: number, k: number): string { + const ans: string[] = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +}