Commit a5b8851 1 parent 3120cfe commit a5b8851 Copy full SHA for a5b8851
File tree 6 files changed +102
-13
lines changed
solution/2600-2699/2696.Minimum String Length After Removing Substrings
6 files changed +102
-13
lines changed Original file line number Diff line number Diff line change @@ -149,17 +149,31 @@ func minLength(s string) int {
149
149
150
150
``` ts
151
151
function minLength(s : string ): number {
152
- const stk: string [] = [' ' ];
152
+ const stk: string [] = [];
153
153
for (const c of s ) {
154
- if (c === ' B ' && stk .at (- 1 )! === ' A ' ) {
154
+ if (( stk . at ( - 1 ) === ' A ' && c === ' B ' ) || ( stk .at (- 1 ) === ' C ' && c === ' D ' ) ) {
155
155
stk .pop ();
156
- } else if (c === ' D' && stk .at (- 1 )! === ' C' ) {
156
+ } else {
157
+ stk .push (c );
158
+ }
159
+ }
160
+ return stk .length ;
161
+ }
162
+ ```
163
+
164
+ #### JavaScript
165
+
166
+ ``` js
167
+ function minLength (s ) {
168
+ const stk = [];
169
+ for (const c of s) {
170
+ if ((stk .at (- 1 ) === ' A' && c === ' B' ) || (stk .at (- 1 ) === ' C' && c === ' D' )) {
157
171
stk .pop ();
158
172
} else {
159
173
stk .push (c);
160
174
}
161
175
}
162
- return stk .length - 1 ;
176
+ return stk .length ;
163
177
}
164
178
```
165
179
@@ -193,4 +207,28 @@ impl Solution {
193
207
194
208
<!-- solution: end -->
195
209
210
+ <!-- solution: start -->
211
+
212
+ ### 方法二:递归(一行代码)
213
+
214
+ <!-- tabs: start -->
215
+
216
+ #### TypeScript
217
+
218
+ ``` ts
219
+ const minLength = (s : string , n = s .length ): number =>
220
+ ((s = s .replace (/ AB| CD/ g , ' ' )), s .length === n ) ? n : minLength (s );
221
+ ```
222
+
223
+ #### JavaScript
224
+
225
+ ``` js
226
+ const minLength = (s , n = s .length ) =>
227
+ ((s = s .replace (/ AB| CD/ g , ' ' )), s .length === n) ? n : minLength (s);
228
+ ```
229
+
230
+ <!-- tabs: end -->
231
+
232
+ <!-- solution: end -->
233
+
196
234
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -147,17 +147,31 @@ func minLength(s string) int {
147
147
148
148
``` ts
149
149
function minLength(s : string ): number {
150
- const stk: string [] = [' ' ];
150
+ const stk: string [] = [];
151
151
for (const c of s ) {
152
- if (c === ' B ' && stk .at (- 1 )! === ' A ' ) {
152
+ if (( stk . at ( - 1 ) === ' A ' && c === ' B ' ) || ( stk .at (- 1 ) === ' C ' && c === ' D ' ) ) {
153
153
stk .pop ();
154
- } else if (c === ' D' && stk .at (- 1 )! === ' C' ) {
154
+ } else {
155
+ stk .push (c );
156
+ }
157
+ }
158
+ return stk .length ;
159
+ }
160
+ ```
161
+
162
+ #### JavaScript
163
+
164
+ ``` js
165
+ function minLength (s ) {
166
+ const stk = [];
167
+ for (const c of s) {
168
+ if ((stk .at (- 1 ) === ' A' && c === ' B' ) || (stk .at (- 1 ) === ' C' && c === ' D' )) {
155
169
stk .pop ();
156
170
} else {
157
171
stk .push (c);
158
172
}
159
173
}
160
- return stk .length - 1 ;
174
+ return stk .length ;
161
175
}
162
176
```
163
177
@@ -191,4 +205,28 @@ impl Solution {
191
205
192
206
<!-- solution: end -->
193
207
208
+ <!-- solution: start -->
209
+
210
+ ### Solution 2: One-liner
211
+
212
+ <!-- tabs: start -->
213
+
214
+ #### TypeScript
215
+
216
+ ``` ts
217
+ const minLength = (s : string , n = s .length ): number =>
218
+ ((s = s .replace (/ AB| CD/ g , ' ' )), s .length === n ) ? n : minLength (s );
219
+ ```
220
+
221
+ #### JavaScript
222
+
223
+ ``` js
224
+ const minLength = (s , n = s .length ) =>
225
+ ((s = s .replace (/ AB| CD/ g , ' ' )), s .length === n) ? n : minLength (s);
226
+ ```
227
+
228
+ <!-- tabs: end -->
229
+
230
+ <!-- solution: end -->
231
+
194
232
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ function minLength ( s ) {
2
+ const stk = [ ] ;
3
+ for ( const c of s ) {
4
+ if ( ( stk . at ( - 1 ) === 'A' && c === 'B' ) || ( stk . at ( - 1 ) === 'C' && c === 'D' ) ) {
5
+ stk . pop ( ) ;
6
+ } else {
7
+ stk . push ( c ) ;
8
+ }
9
+ }
10
+ return stk . length ;
11
+ }
Original file line number Diff line number Diff line change 1
1
function minLength ( s : string ) : number {
2
- const stk : string [ ] = [ '' ] ;
2
+ const stk : string [ ] = [ ] ;
3
3
for ( const c of s ) {
4
- if ( c === 'B' && stk . at ( - 1 ) ! === 'A' ) {
5
- stk . pop ( ) ;
6
- } else if ( c === 'D' && stk . at ( - 1 ) ! === 'C' ) {
4
+ if ( ( stk . at ( - 1 ) === 'A' && c === 'B' ) || ( stk . at ( - 1 ) === 'C' && c === 'D' ) ) {
7
5
stk . pop ( ) ;
8
6
} else {
9
7
stk . push ( c ) ;
10
8
}
11
9
}
12
- return stk . length - 1 ;
10
+ return stk . length ;
13
11
}
Original file line number Diff line number Diff line change
1
+ const minLength = ( s , n = s . length ) =>
2
+ ( ( s = s . replace ( / A B | C D / g, '' ) ) , s . length === n ) ? n : minLength ( s ) ;
Original file line number Diff line number Diff line change
1
+ const minLength = ( s : string , n = s . length ) : number =>
2
+ ( ( s = s . replace ( / A B | C D / g, '' ) ) , s . length === n ) ? n : minLength ( s ) ;
You can’t perform that action at this time.
0 commit comments