Skip to content

Commit cdc26c6

Browse files
committed
Add solution #1208
1 parent 1489cf2 commit cdc26c6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
1122|[Relative Sort Array](./1122-relative-sort-array.js)|Easy|
324324
1189|[Maximum Number of Balloons](./1189-maximum-number-of-balloons.js)|Easy|
325325
1207|[Unique Number of Occurrences](./1207-unique-number-of-occurrences.js)|Easy|
326+
1208|[Get Equal Substrings Within Budget](./1208-get-equal-substrings-within-budget.js)|Medium|
326327
1217|[Minimum Cost to Move Chips to The Same Position](./1217-minimum-cost-to-move-chips-to-the-same-position.js)|Easy|
327328
1232|[Check If It Is a Straight Line](./1232-check-if-it-is-a-straight-line.js)|Easy|
328329
1233|[Remove Sub-Folders from the Filesystem](./1233-remove-sub-folders-from-the-filesystem.js)|Medium|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1208. Get Equal Substrings Within Budget
3+
* https://leetcode.com/problems/get-equal-substrings-within-budget/
4+
* Difficulty: Medium
5+
*
6+
* You are given two strings s and t of the same length and an integer maxCost.
7+
*
8+
* You want to change s to t. Changing the ith character of s to ith character of t costs
9+
* |s[i] - t[i]| (i.e., the absolute difference between the ASCII values of the characters).
10+
*
11+
* Return the maximum length of a substring of s that can be changed to be the same as the
12+
* corresponding substring of t with a cost less than or equal to maxCost. If there is no
13+
* substring from s that can be changed to its corresponding substring from t, return 0.
14+
*/
15+
16+
/**
17+
* @param {string} s
18+
* @param {string} t
19+
* @param {number} maxCost
20+
* @return {number}
21+
*/
22+
var equalSubstring = function(s, t, maxCost) {
23+
let left = -1;
24+
25+
for (let right = 0; right < s.length; right++) {
26+
maxCost -= Math.abs(s.charCodeAt(right) - t.charCodeAt(right));
27+
28+
if (maxCost < 0) {
29+
left++;
30+
maxCost += Math.abs(s.charCodeAt(left) - t.charCodeAt(left));
31+
}
32+
}
33+
34+
return s.length - left - 1;
35+
};

0 commit comments

Comments
 (0)