-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathGet Equal Substrings Within Budget.cpp
48 lines (33 loc) · 1.24 KB
/
Get Equal Substrings Within Budget.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n = s.length();
vector<int> pre(n,0);
pre[0] = abs(s[0] - t[0]);
for(int i = 1; i < n; i++){
pre[i] = pre[i-1] + abs(s[i]-t[i]);
}
int ans = 0, i = -1, j = 0;
while(i<n && j < n){
if(pre[j]- (i < 0 ? 0 : pre[i]) <= maxCost) {
ans = max(ans,j-i);
j++;
}
else{
i++;
}
}
return ans;
}
};