当前位置:网站首页>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] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
- mysql cdc能把能把op字段拿出来吗
- 预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
- Is it safe to open a stock account online in 2022? Is there any danger?
- Suggest collecting | what to do when encountering slow SQL on opengauss?
- SQL SERVER2014删除数据库失败,报错偏移量0x0000...
- It is interesting to understand MMAP in this way!
- Common penetration tools -goby
- Button button clear border
- Submission lottery - light application server essay solicitation activity (may) award announcement
猜你喜欢
[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
The exclusive collection of China lunar exploration project is limited to sale!
Recommend a JSON visualization tool artifact!
12 product management platforms that everyone is using
CRC check
Today in history: the semiconductor war in the late 1990s; Von Neumann published the first draft; CBS acquires CNET
[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming
程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...
Design and practice of new generation cloud native database
推荐一款 JSON 可视化工具神器!
随机推荐
在通达信上买基金安全吗?
Handling distributed transactions with powerful dbpack (PHP tutorial)
Lack of comparator, operational amplifier to save the field! (the op amp is recorded as a comparator circuit)
Matplotlib数据可视化基础
Write your own who commands
SQL Server列一相同的情况下,如何取列二的最大值,并重新生成表
Who's still buying three squirrels
Is it safe to do fund fixed investment on CICC securities?
基金管理人的内部控制
Sqlization is the first step in ETL incremental production. What are the core capabilities of such an architecture?
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
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
A new round of popularity of digital collections opens
【Laravel 】faker数据填充详解
Is it safe to buy funds on the access letter?
使用强大的DBPack处理分布式事务(PHP使用教程)
使用强大的DBPack处理分布式事务(PHP使用教程)
Japanese professor sues Intel FPGA and SOC products for infringing a design patent
dotnet 控制台 使用 Microsoft.Maui.Graphics 配合 Skia 进行绘图入门
Ssh server rejects password, try again; Permitrootlogin yes invalid problem