You are given two strings s
and t
of the same length. You want to change s
to t
. Changing the i
-th character of s
to i
-th character of t
costs |s[i] - t[i]|
that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost
.
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
.
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3 Output: 3 Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0 Output: 1 Explanation: You can't make any change, so the maximum length is 1.
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s
andt
only contain lower case English letters.
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
n = len(s)
presum = [0] * (n + 1)
for i in range(n):
presum[i + 1] = presum[i] + abs(ord(s[i]) - ord(t[i]))
left, right = 0, n
def check(l):
i = 0
while i + l - 1 < n:
j = i + l - 1
if presum[j + 1] - presum[i] <= maxCost:
return True
i += 1
return False
while left < right:
mid = (left + right + 1) >> 1
if check(mid):
left = mid
else:
right = mid - 1
return left
class Solution {
public int equalSubstring(String s, String t, int maxCost) {
int n = s.length();
int[] presum = new int[n + 1];
for (int i = 0; i < n; ++i) {
presum[i + 1] = presum[i] + Math.abs(s.charAt(i) - t.charAt(i));
}
int left = 0, right = n;
while (left < right) {
int mid = (left + right + 1) >>> 1;
if (check(mid, presum, maxCost, n)) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
private boolean check(int l, int[] s, int maxCost, int n) {
int i = 0;
while (i + l - 1 < n) {
int j = i + l - 1;
if (s[j + 1] - s[i] <= maxCost) {
return true;
}
++i;
}
return false;
}
}
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n = s.size();
vector<int> presum(n + 1);
for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + abs(s[i] - t[i]);
int left = 0, right = n;
while (left < right)
{
int mid = left + right + 1 >> 1;
if (check(mid, presum, maxCost, n)) left = mid;
else right = mid - 1;
}
return left;
}
bool check(int l, vector<int>& s, int maxCost, int n) {
int i = 0;
while (i + l - 1 < n)
{
int j = i + l - 1;
if (s[j + 1] - s[i] <= maxCost) return true;
++i;
}
return false;
}
};
func equalSubstring(s string, t string, maxCost int) int {
n := len(s)
presum := make([]int, n+1)
for i, c := range s {
presum[i+1] = presum[i] + abs(int(c)-int(t[i]))
}
left, right := 0, n
check := func(l int) bool {
i := 0
for i+l-1 < n {
j := i + l - 1
if presum[j+1]-presum[i] <= maxCost {
return true
}
i++
}
return false
}
for left < right {
mid := (left + right + 1) >> 1
if check(mid) {
left = mid
} else {
right = mid - 1
}
}
return left
}
func abs(x int) int {
if x > 0 {
return x
}
return -x
}