File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 323
323
1122|[ Relative Sort Array] ( ./1122-relative-sort-array.js ) |Easy|
324
324
1189|[ Maximum Number of Balloons] ( ./1189-maximum-number-of-balloons.js ) |Easy|
325
325
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|
326
327
1217|[ Minimum Cost to Move Chips to The Same Position] ( ./1217-minimum-cost-to-move-chips-to-the-same-position.js ) |Easy|
327
328
1232|[ Check If It Is a Straight Line] ( ./1232-check-if-it-is-a-straight-line.js ) |Easy|
328
329
1233|[ Remove Sub-Folders from the Filesystem] ( ./1233-remove-sub-folders-from-the-filesystem.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments