当前位置:网站首页>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站经典《华强买瓜》
- Sqlization is the first step in ETL incremental production. What are the core capabilities of such an architecture?
- 在通达信上买基金安全吗?
- 零基础入行软件测试必看,10年测试老鸟的良心建议(共15条)
- 新一代云原生数据库的设计与实践
- Design and practice of new generation cloud native database
- 678. 有效的括号字符串
- Does anyone know why? The table structure is the source table MySQL CDC that has just been directly copied
- Kotlin 协程调度切换线程是时候解开真相了
- Does anyone know the logic of limit statement execution in Clickhouse? In the picture, the SQL above can be executed successfully
猜你喜欢

venv: venv 的目录结构

CRC 校驗
![C [byte array] and [hexadecimal string] mutual conversion - codeplus series](/img/d2/dad88f53701c7cd7638bd4983cbb4b.png)
C [byte array] and [hexadecimal string] mutual conversion - codeplus series

CRC verification

Venv: directory structure of venv

华为HMS Core携手超图为三维GIS注入新动能

. Net 5.0+ does not need to rely on third-party native implementation of scheduled tasks

零基础入门测试该学什么?最全整理,照着学就对了

Handling distributed transactions with powerful dbpack (PHP tutorial)
![[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming](/img/85/56b12fd664726e4776cab69ca91d57.png)
[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming
随机推荐
[paper reading] trajectory guided control prediction for end to end autonomous driving: a simple yet strong Ba
中国探月工程独家藏品限量发售!
[.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
[laravel] detailed explanation of faker data filling
Rising stars in Plant Sciences (rsps2022) final Science Lecture (6.30 pm)
dotnet 控制台 使用 Microsoft.Maui.Graphics 配合 Skia 进行绘图入门
Valgrind usage of memory leak locating tool
内存泄漏定位工具之 valgrind 使用
选择在中金证券上炒股开户可以吗?安全吗?
Sqlization is the first step in ETL incremental production. What are the core capabilities of such an architecture?
爬虫(2) - Requests(1) | Requests模块的深度解析
Ssh server rejects password, try again; Permitrootlogin yes invalid problem
Matplotlib data visualization Foundation
CRC 校验
Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
数字藏品市场新局面
Design and practice of new generation cloud native database
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
零基础入门测试该学什么?最全整理,照着学就对了
mysql cdc能把能把op字段拿出来吗