comments | difficulty | edit_url | rating | source | tags | ||||
---|---|---|---|---|---|---|---|---|---|
true |
中等 |
1496 |
第 156 场周赛 Q2 |
|
给你两个长度相同的字符串,s
和 t
。
将 s
中的第 i
个字符变到 t
中的第 i
个字符需要 |s[i] - t[i]|
的开销(开销可能为 0),也就是两个字符的 ASCII 码值的差的绝对值。
用于变更字符串的最大预算是 maxCost
。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。
如果你可以将 s
的子字符串转化为它在 t
中对应的子字符串,则返回可以转化的最大长度。
如果 s
中没有子字符串可以转化成 t
中对应的子字符串,则返回 0
。
示例 1:
输入:s = "abcd", t = "bcdf", maxCost = 3 输出:3 解释:s 中的 "abc" 可以变为 "bcd"。开销为 3,所以最大长度为 3。
示例 2:
输入:s = "abcd", t = "cdef", maxCost = 3
输出:1
解释:s 中的任一字符要想变成 t 中对应的字符,其开销都是 2。因此,最大长度为 1。
示例 3:
输入:s = "abcd", t = "acde", maxCost = 0 输出:1 解释:a -> a, cost = 0,字符串未发生变化,所以最大长度为 1。
提示:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s
和t
都只含小写英文字母。
我们可以创建一个长度为
注意到长度具有单调性,即如果存在长度为
我们定义函数 true
,否则返回 false
。
接下来,我们定义二分查找的左边界 true
,那么我们将左边界更新为
时间复杂度
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
def check(x):
for i in range(n):
j = i + mid - 1
if j < n and f[j + 1] - f[i] <= maxCost:
return True
return False
n = len(s)
f = list(accumulate((abs(ord(a) - ord(b)) for a, b in zip(s, t)), initial=0))
l, r = 0, n
while l < r:
mid = (l + r + 1) >> 1
if check(mid):
l = mid
else:
r = mid - 1
return l
class Solution {
private int maxCost;
private int[] f;
private int n;
public int equalSubstring(String s, String t, int maxCost) {
n = s.length();
f = new int[n + 1];
this.maxCost = maxCost;
for (int i = 0; i < n; ++i) {
int x = Math.abs(s.charAt(i) - t.charAt(i));
f[i + 1] = f[i] + x;
}
int l = 0, r = n;
while (l < r) {
int mid = (l + r + 1) >>> 1;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
private boolean check(int x) {
for (int i = 0; i + x - 1 < n; ++i) {
int j = i + x - 1;
if (f[j + 1] - f[i] <= maxCost) {
return true;
}
}
return false;
}
}
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n = s.size();
int f[n + 1];
f[0] = 0;
for (int i = 0; i < n; ++i) {
f[i + 1] = f[i] + abs(s[i] - t[i]);
}
auto check = [&](int x) -> bool {
for (int i = 0; i + x - 1 < n; ++i) {
int j = i + x - 1;
if (f[j + 1] - f[i] <= maxCost) {
return true;
}
}
return false;
};
int l = 0, r = n;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
};
func equalSubstring(s string, t string, maxCost int) int {
n := len(s)
f := make([]int, n+1)
for i, a := range s {
f[i+1] = f[i] + abs(int(a)-int(t[i]))
}
check := func(x int) bool {
for i := 0; i+x-1 < n; i++ {
if f[i+x]-f[i] <= maxCost {
return true
}
}
return false
}
l, r := 0, n
for l < r {
mid := (l + r + 1) >> 1
if check(mid) {
l = mid
} else {
r = mid - 1
}
}
return l
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
function equalSubstring(s: string, t: string, maxCost: number): number {
const n = s.length;
const f = Array(n + 1).fill(0);
for (let i = 0; i < n; i++) {
f[i + 1] = f[i] + Math.abs(s.charCodeAt(i) - t.charCodeAt(i));
}
const check = (x: number): boolean => {
for (let i = 0; i + x - 1 < n; i++) {
if (f[i + x] - f[i] <= maxCost) {
return true;
}
}
return false;
};
let l = 0,
r = n;
while (l < r) {
const mid = (l + r + 1) >> 1;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
我们可以维护两个指针
最后返回答案即可。
时间复杂度
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
n = len(s)
ans = cost = l = 0
for r in range(n):
cost += abs(ord(s[r]) - ord(t[r]))
while cost > maxCost:
cost -= abs(ord(s[l]) - ord(t[l]))
l += 1
ans = max(ans, r - l + 1)
return ans
class Solution {
public int equalSubstring(String s, String t, int maxCost) {
int n = s.length();
int ans = 0, cost = 0;
for (int l = 0, r = 0; r < n; ++r) {
cost += Math.abs(s.charAt(r) - t.charAt(r));
while (cost > maxCost) {
cost -= Math.abs(s.charAt(l) - t.charAt(l));
++l;
}
ans = Math.max(ans, r - l + 1);
}
return ans;
}
}
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n = s.length();
int ans = 0, cost = 0;
for (int l = 0, r = 0; r < n; ++r) {
cost += abs(s[r] - t[r]);
while (cost > maxCost) {
cost -= abs(s[l] - t[l]);
++l;
}
ans = max(ans, r - l + 1);
}
return ans;
}
};
func equalSubstring(s string, t string, maxCost int) (ans int) {
var cost, l int
for r := range s {
cost += abs(int(s[r]) - int(t[r]))
for ; cost > maxCost; l++ {
cost -= abs(int(s[l]) - int(t[l]))
}
ans = max(ans, r-l+1)
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
function equalSubstring(s: string, t: string, maxCost: number): number {
const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0));
const n = s.length;
let ans = 0,
cost = 0;
for (let l = 0, r = 0; r < n; ++r) {
cost += getCost(r);
while (cost > maxCost) {
cost -= getCost(l++);
}
ans = Math.max(ans, r - l + 1);
}
return ans;
}
在方法二中,双指针维护的区间可能变短,也可能变长,由于题目只需要求出最大长度,我们可以维护一个单调变长的区间。
具体地,我们用两个指针
最后返回
时间复杂度
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
cost = l = 0
for a, b in zip(s, t):
cost += abs(ord(a) - ord(b))
if cost > maxCost:
cost -= abs(ord(s[l]) - ord(t[l]))
l += 1
return len(s) - l
class Solution {
public int equalSubstring(String s, String t, int maxCost) {
int n = s.length();
int cost = 0, l = 0;
for (int r = 0; r < n; ++r) {
cost += Math.abs(s.charAt(r) - t.charAt(r));
if (cost > maxCost) {
cost -= Math.abs(s.charAt(l) - t.charAt(l));
++l;
}
}
return n - l;
}
}
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n = s.length();
int cost = 0, l = 0;
for (int r = 0; r < n; ++r) {
cost += abs(s[r] - t[r]);
if (cost > maxCost) {
cost -= abs(s[l] - t[l]);
++l;
}
}
return n - l;
}
};
func equalSubstring(s string, t string, maxCost int) int {
n := len(s)
var cost, l int
for r := range s {
cost += abs(int(s[r]) - int(t[r]))
if cost > maxCost {
cost -= abs(int(s[l]) - int(t[l]))
l++
}
}
return n - l
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
function equalSubstring(s: string, t: string, maxCost: number): number {
const getCost = (i: number) => Math.abs(s[i].charCodeAt(0) - t[i].charCodeAt(0));
const n = s.length;
let cost = 0;
let l = 0;
for (let r = 0; r < n; ++r) {
cost += getCost(r);
if (cost > maxCost) {
cost -= getCost(l++);
}
}
return n - l;
}