-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
34 lines (30 loc) · 1.17 KB
/
Solution.cs
File metadata and controls
34 lines (30 loc) · 1.17 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
namespace LeetCode.Medium.Problem1208{
//1208. Get Equal Substrings Within Budget
//https://leetcode.com/problems/get-equal-substrings-within-budget/
/*
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.
*/
public class Solution {
public int EqualSubstring(string s, string t, int maxCost) {
int counter = 0;
int left = 0;
int ans = 0;
for (int right = 0; right < s.Length; right++)
{
counter += Math.Abs(t[right] - s[right]);
while (counter > maxCost)
{
counter -= Math.Abs(t[left] - s[left]);
left++;
}
ans = Math.Max(ans, right - left + 1);
}
return ans;
}
}
}