2024/10/18 灵茶题单 不定长滑动窗口

This commit is contained in:
Cool 2024-10-18 12:37:33 +08:00
parent ff4ce5ceab
commit eeaac8389c
1 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package com.cool.ling_cha_mount.sliding_windows;
import org.junit.Test;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/10/18/12:28
* @Description: 1208. 尽可能使字符串相等
* Hard 2
* Score 1497
*/
public class Num1208 {
public int equalSubstring(String s, String t, int maxCost) {
int curCost = maxCost;
int left = 0;
int res = 0;
char[] sChar = s.toCharArray();
char[] tChar = t.toCharArray();
for (int i = 0; i < sChar.length; i++) {
curCost -= Math.abs(sChar[i] - tChar[i]);
while (curCost < 0) {
curCost+=Math.abs(sChar[left]-tChar[left++]);
}
res = Math.max(res, i - left + 1);
}
return res;
}
@Test
public void test() {
equalSubstring("krrgw", "zjxss", 19);
}
}