File tree 3 files changed +21
-30
lines changed
solution/2400-2499/2486.Append Characters to String to Make Subsequence
3 files changed +21
-30
lines changed Original file line number Diff line number Diff line change @@ -154,17 +154,14 @@ func appendCharacters(s string, t string) int {
154
154
155
155
``` ts
156
156
function appendCharacters(s : string , t : string ): number {
157
- const [m, n] = [s .length , t .length ];
158
- for (let i = 0 , j = 0 ; j < n ; ++ j ) {
159
- while (i < m && s [i ] !== t [j ]) {
160
- ++ i ;
161
- }
162
- if (i === m ) {
163
- return n - j ;
164
- }
165
- ++ i ;
157
+ const n = s .length ;
158
+ let j = 0 ;
159
+
160
+ for (let i = 0 ; i < n ; i ++ ) {
161
+ if (s [i ] === t [j ]) j ++ ;
166
162
}
167
- return 0 ;
163
+
164
+ return t .length - j ;
168
165
}
169
166
```
170
167
Original file line number Diff line number Diff line change @@ -152,17 +152,14 @@ func appendCharacters(s string, t string) int {
152
152
153
153
``` ts
154
154
function appendCharacters(s : string , t : string ): number {
155
- const [m, n] = [s .length , t .length ];
156
- for (let i = 0 , j = 0 ; j < n ; ++ j ) {
157
- while (i < m && s [i ] !== t [j ]) {
158
- ++ i ;
159
- }
160
- if (i === m ) {
161
- return n - j ;
162
- }
163
- ++ i ;
155
+ const n = s .length ;
156
+ let j = 0 ;
157
+
158
+ for (let i = 0 ; i < n ; i ++ ) {
159
+ if (s [i ] === t [j ]) j ++ ;
164
160
}
165
- return 0 ;
161
+
162
+ return t .length - j ;
166
163
}
167
164
```
168
165
Original file line number Diff line number Diff line change 1
1
function appendCharacters ( s : string , t : string ) : number {
2
- const [ m , n ] = [ s . length , t . length ] ;
3
- for ( let i = 0 , j = 0 ; j < n ; ++ j ) {
4
- while ( i < m && s [ i ] !== t [ j ] ) {
5
- ++ i ;
6
- }
7
- if ( i === m ) {
8
- return n - j ;
9
- }
10
- ++ i ;
2
+ const n = s . length ;
3
+ let j = 0 ;
4
+
5
+ for ( let i = 0 ; i < n ; i ++ ) {
6
+ if ( s [ i ] === t [ j ] ) j ++ ;
11
7
}
12
- return 0 ;
8
+
9
+ return t . length - j ;
13
10
}
You can’t perform that action at this time.
0 commit comments