-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1208-get-equal-substrings-within-budget.js
More file actions
35 lines (31 loc) · 1.09 KB
/
1208-get-equal-substrings-within-budget.js
File metadata and controls
35 lines (31 loc) · 1.09 KB
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
28
29
30
31
32
33
34
35
/**
* 1208. Get Equal Substrings Within Budget
* https://leetcode.com/problems/get-equal-substrings-within-budget/
* Difficulty: Medium
*
* You are given two strings s and t of the same length and an integer maxCost.
*
* You want to change s to t. Changing the ith character of s to ith character of t costs
* |s[i] - t[i]| (i.e., the absolute difference between the ASCII values of the characters).
*
* Return the maximum length of a substring of s that can be changed to be the same as the
* corresponding substring of t with a cost less than or equal to maxCost. If there is no
* substring from s that can be changed to its corresponding substring from t, return 0.
*/
/**
* @param {string} s
* @param {string} t
* @param {number} maxCost
* @return {number}
*/
var equalSubstring = function(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;
};