当前位置:网站首页>LeetCode 438. 找到字符串中所有字母异位词__滑动窗口
LeetCode 438. 找到字符串中所有字母异位词__滑动窗口
2022-07-01 10:42:00 【向光.】
438. 找到字符串中所有字母异位词
给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引。不考虑答案输出的顺序。
异位词 指由相同字母重排列形成的字符串(包括相同的字符串)。
示例 1:
输入: s = "cbaebabacd", p = "abc"
输出: [0,6]
解释:
起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词。
起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词。
示例 2:
输入: s = "abab", p = "ab"
输出: [0,1,2]
解释:
起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词。
起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词。
起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词。
提示:
1 <= s.length, p.length <= 3 * 104
s 和 p 仅包含小写字母
Solution1(直白滑窗):
显然这题我们可以使用滑动窗口来解决,即直接维护p字符串长度的窗口,每当窗口满时就判断一下窗口内的各种字母数量是否和p的一致即可。
Code1:
class Solution {
public List<Integer> findAnagrams(String s, String p) {
int[] hash = new int[26];
int winLen = p.length();
for(int i=0;i<winLen;i++){
char c = p.charAt(i);
hash[c - 'a']++;
}
List<Integer> res = new ArrayList<>();
List<Integer> window = new ArrayList<>();
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(window.size() < winLen){
window.add(i);
}
else{
check(res,winLen,window,hash,s);
window.remove((int)0);
i--;
}
}
if(window.size() < winLen){
return res;
}
check(res,winLen,window,hash,s);
return res;
}
public void check(List<Integer> res,int winLen,List<Integer> window,int[] hash,String s){
int[] hashTemp = new int[26];
for(int k=0;k<winLen;k++){
Integer index = window.get(k);
hashTemp[s.charAt(index) - 'a']++;
}
int j = 0;
for(;j<26;j++){
if(hash[j] != hashTemp[j])
break;
}
if(j == 26){
res.add(window.get(0));
}
}
}
Solution2(优化便捷滑窗):
我们可以不直接维护普通滑窗,因为我们发现上述方法一,由于是维护一个有序序列来模拟滑窗,后面还需要一个哈希数组来表示滑窗内含有的字母种类及数量,因此我们可以想只创建一次哈希数组即可,在滑窗滑出老元素和滑进新元素时直接在那一个哈希数组上进行修改就行。
接着我们发现,可以直接使用哈希数组代替滑窗的有序序列进行存储,只用两个下标来模拟滑窗即可,滑窗本身不需要存储内容,交给哈希数组存储即可。
Code2:
class Solution {
public List<Integer> findAnagrams(String s, String p) {
int slen = s.length();
int plen = p.length();
List<Integer> res = new ArrayList<>();
if(s.length() < p.length()){
return res;
}
int[] hash = new int[26];
int[] hashTemp = new int[26];
// 先给滑窗塞满
for(int i=0;i<plen;i++){
hash[p.charAt(i) - 'a']++;
hashTemp[s.charAt(i) - 'a']++;
}
if(Arrays.equals(hash,hashTemp)){
res.add(0);
}
// 维护滑窗的两个下标即可,一个头部,一个头部+滑窗长度即可
for(int i=0;i<slen - plen ;i++){
hashTemp[s.charAt(i) - 'a']--;
hashTemp[s.charAt(i + plen) - 'a']++;
if(Arrays.equals(hash,hashTemp)){
res.add(i+1);
}
}
return res;
}
}
边栏推荐
- [.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
- How to solve the problem of SQL?
- CRC 校验
- STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
- 【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Ba
- 基金管理人的内部控制
- 零基础入行软件测试必看,10年测试老鸟的良心建议(共15条)
- Kotlin 协程调度切换线程是时候解开真相了
- Kotlin coprocessor scheduling switch threads it's time to unravel the truth
- Today in history: the semiconductor war in the late 1990s; Von Neumann published the first draft; CBS acquires CNET
猜你喜欢

A new round of popularity of digital collections opens
![[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul](/img/58/8d5c78d919ed60bc833ec4daa22e23.jpg)
[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul

Error: missing revert data in call exception

北汽蓝谷:业绩承压,极狐难期

C one line code calculates the MD5 value of the file - codeplus series

Programmers want to go to state-owned enterprises? The technology is backward and the salary is low. I can't find a job after lying flat for several years

云上“视界” 创新无限 | 2022阿里云直播峰会正式上线

【MPC】②quadprog求解正定、半正定、负定二次规划

The exclusive collection of China lunar exploration project is limited to sale!

Recommend a JSON visualization tool artifact!
随机推荐
零基础入门测试该学什么?最全整理,照着学就对了
4hutool practice: dateutil format time [easy to understand]
Error: missing revert data in call exception
大佬们,数据湖iceberg的数据,怎样导出到mysql? 有什么工具? sqoop,datax都没
Introduction of uniapp wechat applet components on demand
Kotlin 协程调度切换线程是时候解开真相了
Design and practice of new generation cloud native database
建议收藏 | 在openGauss上遇到慢SQL该怎么办?
Kotlin coprocessor scheduling switch threads it's time to unravel the truth
Handling distributed transactions with powerful dbpack (PHP tutorial)
It is interesting to understand MMAP in this way!
程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...
The exclusive collection of China lunar exploration project is limited to sale!
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
NC | intestinal cells and lactic acid bacteria work together to prevent Candida infection
[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming
Write your own who commands
[encounter Django] - (II) database configuration
JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan! Samsung sk of South Korea competes for salary increase to retain semiconductor talents;
推荐一款 JSON 可视化工具神器!