File tree Expand file tree Collapse file tree 4 files changed +61
-12
lines changed
solution/1900-1999/1957.Delete Characters to Make Fancy String Expand file tree Collapse file tree 4 files changed +61
-12
lines changed Original file line number Diff line number Diff line change @@ -156,6 +156,34 @@ function makeFancyString(s: string): string {
156156}
157157```
158158
159+ #### TypeScript
160+
161+ ``` ts
162+ function makeFancyString(s : string ): string {
163+ let [n, ans] = [s .length , ' ' ];
164+ for (let i = 0 ; i < n ; i ++ ) {
165+ if (s [i ] !== s [i - 1 ] || s [i ] !== s [i - 2 ]) {
166+ ans += s [i ];
167+ }
168+ }
169+ return ans ;
170+ }
171+ ```
172+
173+ #### JavaScript
174+
175+ ``` js
176+ function makeFancyString (s ) {
177+ let [n, ans] = [s .length , ' ' ];
178+ for (let i = 0 ; i < n; i++ ) {
179+ if (s[i] !== s[i - 1 ] || s[i] !== s[i - 2 ]) {
180+ ans += s[i];
181+ }
182+ }
183+ return ans;
184+ }
185+ ```
186+
159187#### PHP
160188
161189``` php
Original file line number Diff line number Diff line change @@ -143,14 +143,27 @@ func makeFancyString(s string) string {
143143
144144``` ts
145145function makeFancyString(s : string ): string {
146- const ans: string [] = [];
147- for (const c of s ) {
148- const n = ans .length ;
149- if (n < 2 || c !== ans [n - 1 ] || c !== ans [n - 2 ]) {
150- ans .push (c );
146+ let [n, ans] = [s .length , ' ' ];
147+ for (let i = 0 ; i < n ; i ++ ) {
148+ if (s [i ] !== s [i - 1 ] || s [i ] !== s [i - 2 ]) {
149+ ans += s [i ];
151150 }
152151 }
153- return ans .join (' ' );
152+ return ans ;
153+ }
154+ ```
155+
156+ #### JavaScript
157+
158+ ``` js
159+ function makeFancyString (s ) {
160+ let [n, ans] = [s .length , ' ' ];
161+ for (let i = 0 ; i < n; i++ ) {
162+ if (s[i] !== s[i - 1 ] || s[i] !== s[i - 2 ]) {
163+ ans += s[i];
164+ }
165+ }
166+ return ans;
154167}
155168```
156169
Original file line number Diff line number Diff line change 1+ function makeFancyString ( s ) {
2+ let [ n , ans ] = [ s . length , '' ] ;
3+ for ( let i = 0 ; i < n ; i ++ ) {
4+ if ( s [ i ] !== s [ i - 1 ] || s [ i ] !== s [ i - 2 ] ) {
5+ ans += s [ i ] ;
6+ }
7+ }
8+ return ans ;
9+ }
Original file line number Diff line number Diff line change 11function makeFancyString ( s : string ) : string {
2- const ans : string [ ] = [ ] ;
3- for ( const c of s ) {
4- const n = ans . length ;
5- if ( n < 2 || c !== ans [ n - 1 ] || c !== ans [ n - 2 ] ) {
6- ans . push ( c ) ;
2+ let [ n , ans ] = [ s . length , '' ] ;
3+ for ( let i = 0 ; i < n ; i ++ ) {
4+ if ( s [ i ] !== s [ i - 1 ] || s [ i ] !== s [ i - 2 ] ) {
5+ ans += s [ i ] ;
76 }
87 }
9- return ans . join ( '' ) ;
8+ return ans ;
109}
You can’t perform that action at this time.
0 commit comments