-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path1208.js
More file actions
27 lines (26 loc) · 754 Bytes
/
Copy path1208.js
File metadata and controls
27 lines (26 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const equalSubstring = (s, t, maxCost) => {
let max = 0;
for (let left = -1, right = 0; right < s.length; ++right) {
maxCost -= Math.abs(s.charCodeAt(right) - t.charCodeAt(right));
if (maxCost >= 0) {
right - left > max && (max = right - left);
} else {
while (maxCost < 0) {
++left;
maxCost += Math.abs(s.charCodeAt(left) - t.charCodeAt(left));
}
}
}
return max;
};
const equalSubstring = (s, t, maxCost) => {
let left = -1;
for (let right = 0; right < s.length; ++right) {
maxCost -= Math.abs(s.charCodeAt(right) - t.charCodeAt(right));
if (maxCost < 0) {
++left;
maxCost += Math.abs(s.charCodeAt(left) - t.charCodeAt(left));
}
}
return s.length - left - 1;
};