2024/10/18 灵茶题单 不定长滑动窗口
This commit is contained in:
parent
ff4ce5ceab
commit
eeaac8389c
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue