File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed
solution/1200-1299/1208.Get Equal Substrings Within Budget Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -322,6 +322,28 @@ func abs(x int) int {
322
322
}
323
323
```
324
324
325
+ #### TypeScript
326
+
327
+ ``` ts
328
+ function equalSubstring(s : string , t : string , maxCost : number ): number {
329
+ const getCost = (i : number ) => Math .abs (s [i ].charCodeAt (0 ) - t [i ].charCodeAt (0 ));
330
+ const n = s .length ;
331
+ let res = 0 ;
332
+ let l = 0 ;
333
+ let r = - 1 ;
334
+ let cost = 0 ;
335
+
336
+ while (++ r < n ) {
337
+ cost += getCost (r );
338
+
339
+ if (cost <= maxCost ) res = Math .max (res , r - l + 1 );
340
+ else cost -= getCost (l ++ );
341
+ }
342
+
343
+ return res ;
344
+ }
345
+ ```
346
+
325
347
<!-- tabs: end -->
326
348
327
349
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -319,6 +319,28 @@ func abs(x int) int {
319
319
}
320
320
```
321
321
322
+ #### TypeScript
323
+
324
+ ``` ts
325
+ function equalSubstring(s : string , t : string , maxCost : number ): number {
326
+ const getCost = (i : number ) => Math .abs (s [i ].charCodeAt (0 ) - t [i ].charCodeAt (0 ));
327
+ const n = s .length ;
328
+ let res = 0 ;
329
+ let l = 0 ;
330
+ let r = - 1 ;
331
+ let cost = 0 ;
332
+
333
+ while (++ r < n ) {
334
+ cost += getCost (r );
335
+
336
+ if (cost <= maxCost ) res = Math .max (res , r - l + 1 );
337
+ else cost -= getCost (l ++ );
338
+ }
339
+
340
+ return res ;
341
+ }
342
+ ```
343
+
322
344
<!-- tabs: end -->
323
345
324
346
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ function equalSubstring ( s : string , t : string , maxCost : number ) : number {
2
+ const getCost = ( i : number ) => Math . abs ( s [ i ] . charCodeAt ( 0 ) - t [ i ] . charCodeAt ( 0 ) ) ;
3
+ const n = s . length ;
4
+ let res = 0 ;
5
+ let l = 0 ;
6
+ let r = - 1 ;
7
+ let cost = 0 ;
8
+
9
+ while ( ++ r < n ) {
10
+ cost += getCost ( r ) ;
11
+
12
+ if ( cost <= maxCost ) res = Math . max ( res , r - l + 1 ) ;
13
+ else cost -= getCost ( l ++ ) ;
14
+ }
15
+
16
+ return res ;
17
+ }
You can’t perform that action at this time.
0 commit comments