当前位置:网站首页>leetcode-08
leetcode-08
2022-07-19 10:53:00 【Fairy wants carry】
The sliding window

class Solution {
public static List<Integer> findAnagrams(String s,String p){
int slength=s.length();
int plength=p.length();
ArrayList<Integer> result = new ArrayList<>();
if(slength<plength) return result;
int[] sCnt = new int[26];
int[] pCnt = new int[26];
//1. initialization p Word frequency array and s Word frequency array of , Next two positions
for (int i = 0; i < plength; i++) {
pCnt[p.charAt(i)-'a']++;
sCnt[s.charAt(i)-'a']++;
}
//2. Make an initial judgment
if (Arrays.equals(sCnt,pCnt)){
result.add(0);
}
//3. Traverse p Judge , Because I judged once before , Clear the left side every time you judge , Then move to the right once , Make a second judgment
for (int i =plength; i < slength; i++) {
sCnt[s.charAt(i-plength)-'a']--;
sCnt[s.charAt(i)-'a']++;
if (Arrays.equals(sCnt, pCnt)) {
result.add(i-plength+1);
}
}
return result;
}
}
class Solution {
public int characterReplacement(String s, int k) {
int[] num = new int[26];
int n = s.length();
int maxn = 0;
//left: Left boundary , Used to subtract the head or calculate the length when sliding
//right: Right border , Used to add a window tail or calculate the length
int left = 0, right = 0;
while (right < n) {
int indexR = s.charAt(right) - 'A';
num[indexR]++;
// Find the maximum number of times a letter has appeared in the window
// Calculate the maximum number of times a letter appears in a window , The window length can only be increased or unchanged ( Pay attention to the back left The pointer just moved 0-1 Time )
// The significance of doing so : We want the longest , If you can't find a longer hold length, the return result will not be affected
maxn = Math.max(maxn, num[indexR]);
// length len=right-left+1, hereinafter referred to as len
//len- Maximum number of letter occurrences > Number of replacements => len> Maximum number of letter occurrences + Number of replacements
// Look at the , The number of substitutions is constant =k, The maximum number of letters is possible to change , therefore , Only the maximum number of letters increases ,len To get the maximum
// Without meeting the conditions ,left and right Move together ,len constant
if (right - left + 1 - maxn > k) {
// Here we need to reduce , because left Cross this point , Will affect the maximum value
num[s.charAt(left) - 'A']--;
left++;
}
// After walking here , Actually right Will take one more step
right++;
}
// because right Take one more step , The result is (right-1)-left+1==right-left
return right - left;
}
}
边栏推荐
- LeetCode 2315. 统计星号(字符串)
- 开发第一个Flink应用
- [leetcode weekly replay] 302 weekly 20220717
- Custom complex logic verification during adding and modifying -2022 new project
- 最大半连通子图(tarjan缩点+拓扑排序+dp最长链)
- (二)使用MySQL
- [csp-j 2021] summary
- LeetCode 2335. Minimum total time required to fill the cup
- 人大、微软等提出InclusiveFL:异构设备上的包容性联邦学习
- [PostgreSQL] PostgreSQL 15 optimizes distinct
猜你喜欢
随机推荐
开发第一个Flink应用
人大、微软等提出InclusiveFL:异构设备上的包容性联邦学习
基于“7·20郑州特大暴雨”对空天地一体化通信的思考
Satellite network capacity improvement method based on network coding
vulnhub inclusiveness: 1
c语言指针的有关总结
Google Earth Engine APP(GEE)—设定中国区域的一个夜间灯光时序分析app
LeetCode 2249. 统计圆内格点数目
Pytoch realizes multi-layer perceptron manually
新增、修改操作时自定义复杂逻辑校验-2022新项目
LeetCode 2335. Minimum total time required to fill the cup
基于网络编码的卫星网络容量提升方法
Thread pool principle
关于hping打流测试工具
移植吴恩达深度学习01机器学习和神经网络第二周神经网络基础编程作业选修作业到pycharm
使用tesseract.js-offline识别图片文字记录
Design and Simulation of intelligent storage cabinet control system
二叉树刷题(二)
Brush questions with binary tree (2)
leetcode-08








