Skip to content

feat: add solutions to lc problem: No.1208 #2932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 28, 2024
Next Next commit
feat: add TypeScript solution to lc problem: No.1208
  • Loading branch information
rain84 committed May 27, 2024
commit a48fd87135b7d9ab98af5c51a9887bccf93ad037
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,28 @@ func abs(x int) int {
}
```

#### TypeScript

```ts
function equalSubstring(s: string, t: string, maxCost: number): number {
const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0));
const n = s.length;
let res = 0;
let l = 0;
let r = -1;
let cost = 0;

while (++r < n) {
cost += getCost(r);

if (cost <= maxCost) res = Math.max(res, r - l + 1);
else cost -= getCost(l++);
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,28 @@ func abs(x int) int {
}
```

#### TypeScript

```ts
function equalSubstring(s: string, t: string, maxCost: number): number {
const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0));
const n = s.length;
let res = 0;
let l = 0;
let r = -1;
let cost = 0;

while (++r < n) {
cost += getCost(r);

if (cost <= maxCost) res = Math.max(res, r - l + 1);
else cost -= getCost(l++);
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function equalSubstring(s: string, t: string, maxCost: number): number {
const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0));
const n = s.length;
let res = 0;
let l = 0;
let r = -1;
let cost = 0;

while (++r < n) {
cost += getCost(r);

if (cost <= maxCost) res = Math.max(res, r - l + 1);
else cost -= getCost(l++);
}

return res;
}