Skip to content

Commit a48fd87

Browse files
committed
feat: add TypeScript solution to lc problem: No.1208
1 parent e323d06 commit a48fd87

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

solution/1200-1299/1208.Get Equal Substrings Within Budget/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,28 @@ func abs(x int) int {
322322
}
323323
```
324324

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+
325347
<!-- tabs:end -->
326348

327349
<!-- solution:end -->

solution/1200-1299/1208.Get Equal Substrings Within Budget/README_EN.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,28 @@ func abs(x int) int {
319319
}
320320
```
321321

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+
322344
<!-- tabs:end -->
323345

324346
<!-- solution:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

0 commit comments

Comments
 (0)