File tree 4 files changed +134
-0
lines changed
solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n
4 files changed +134
-0
lines changed Original file line number Diff line number Diff line change @@ -159,6 +159,54 @@ public:
159
159
};
160
160
```
161
161
162
+ #### TypeScript
163
+
164
+ ``` ts
165
+ function getHappyString(n : number , k : number ): string {
166
+ const ans: string [] = [];
167
+
168
+ const dfs = (s = ' ' ) => {
169
+ if (s .length === n ) {
170
+ ans .push (s );
171
+ return ;
172
+ }
173
+
174
+ for (const ch of ' abc' ) {
175
+ if (s .at (- 1 ) === ch ) continue ;
176
+ dfs (s + ch );
177
+ }
178
+ };
179
+
180
+ dfs ();
181
+
182
+ return ans [k - 1 ] ?? ' ' ;
183
+ }
184
+ ```
185
+
186
+ #### JavaScript
187
+
188
+ ``` js
189
+ function getHappyString (n , k ) {
190
+ const ans = [];
191
+
192
+ const dfs = (s = ' ' ) => {
193
+ if (s .length === n) {
194
+ ans .push (s);
195
+ return ;
196
+ }
197
+
198
+ for (const ch of ' abc' ) {
199
+ if (s .at (- 1 ) === ch) continue ;
200
+ dfs (s + ch);
201
+ }
202
+ };
203
+
204
+ dfs ();
205
+
206
+ return ans[k - 1 ] ?? ' ' ;
207
+ }
208
+ ` ` `
209
+
162
210
<!-- tabs:end -->
163
211
164
212
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -146,6 +146,54 @@ public:
146
146
};
147
147
```
148
148
149
+ #### TypeScript
150
+
151
+ ``` ts
152
+ function getHappyString(n : number , k : number ): string {
153
+ const ans: string [] = [];
154
+
155
+ const dfs = (s = ' ' ) => {
156
+ if (s .length === n ) {
157
+ ans .push (s );
158
+ return ;
159
+ }
160
+
161
+ for (const ch of ' abc' ) {
162
+ if (s .at (- 1 ) === ch ) continue ;
163
+ dfs (s + ch );
164
+ }
165
+ };
166
+
167
+ dfs ();
168
+
169
+ return ans [k - 1 ] ?? ' ' ;
170
+ }
171
+ ```
172
+
173
+ #### JavaScript
174
+
175
+ ``` js
176
+ function getHappyString (n , k ) {
177
+ const ans = [];
178
+
179
+ const dfs = (s = ' ' ) => {
180
+ if (s .length === n) {
181
+ ans .push (s);
182
+ return ;
183
+ }
184
+
185
+ for (const ch of ' abc' ) {
186
+ if (s .at (- 1 ) === ch) continue ;
187
+ dfs (s + ch);
188
+ }
189
+ };
190
+
191
+ dfs ();
192
+
193
+ return ans[k - 1 ] ?? ' ' ;
194
+ }
195
+ ` ` `
196
+
149
197
<!-- tabs:end -->
150
198
151
199
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ function getHappyString ( n , k ) {
2
+ const ans = [ ] ;
3
+
4
+ const dfs = ( s = '' ) => {
5
+ if ( s . length === n ) {
6
+ ans . push ( s ) ;
7
+ return ;
8
+ }
9
+
10
+ for ( const ch of 'abc' ) {
11
+ if ( s . at ( - 1 ) === ch ) continue ;
12
+ dfs ( s + ch ) ;
13
+ }
14
+ } ;
15
+
16
+ dfs ( ) ;
17
+
18
+ return ans [ k - 1 ] ?? '' ;
19
+ }
Original file line number Diff line number Diff line change
1
+ function getHappyString ( n : number , k : number ) : string {
2
+ const ans : string [ ] = [ ] ;
3
+
4
+ const dfs = ( s = '' ) => {
5
+ if ( s . length === n ) {
6
+ ans . push ( s ) ;
7
+ return ;
8
+ }
9
+
10
+ for ( const ch of 'abc' ) {
11
+ if ( s . at ( - 1 ) === ch ) continue ;
12
+ dfs ( s + ch ) ;
13
+ }
14
+ } ;
15
+
16
+ dfs ( ) ;
17
+
18
+ return ans [ k - 1 ] ?? '' ;
19
+ }
You can’t perform that action at this time.
0 commit comments