当前位置:网站首页>LeetCode 438. Find all letter ectopic words in the string__ sliding window
LeetCode 438. Find all letter ectopic words in the string__ sliding window
2022-07-01 10:47:00 【To the light】
438. Find all alphabetic words in the string
Given two strings s and p, find s All in p Of Heterotopic words The string of , Returns the starting index of these substrings . Regardless of the order of the answer output .
Heterotopic words A string formed by rearrangement of the same letters ( Include the same string ).
Example 1:
Input : s = "cbaebabacd", p = "abc"
Output : [0,6]
explain :
Start index equals 0 The substring is "cba", It is "abc" The heterotopic words of .
Start index equals 6 The substring is "bac", It is "abc" The heterotopic words of .
Example 2:
Input : s = "abab", p = "ab"
Output : [0,1,2]
explain :
Start index equals 0 The substring is "ab", It is "ab" The heterotopic words of .
Start index equals 1 The substring is "ba", It is "ab" The heterotopic words of .
Start index equals 2 The substring is "ab", It is "ab" The heterotopic words of .
Tips :
1 <= s.length, p.length <= 3 * 104
s and p Only lowercase letters
Solution1( Straight sliding window ):
Obviously, we can use sliding window to solve this problem , Direct maintenance p String length window , When the window is full, judge whether the number of letters in the window is the same as p The consistency of the .
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( Optimize convenient sliding window ):
We can not directly maintain ordinary sliding windows , Because we found the above method one , Because it is to maintain an ordered sequence to simulate sliding windows , A hash array is also needed to indicate the type and number of letters contained in the sliding window , So we can just create a hash array once , When sliding out the old element and sliding in the new element, you can directly modify the hash array .
Then we found , You can directly use a hash array instead of an ordered sequence of sliding windows for storage , Just use two subscripts to simulate sliding window , The sliding window itself does not need to store content , Just give it to the hash array for storage .
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];
// Fill the sliding window first
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);
}
// Maintain the two subscripts of the sliding window , A head , A head + The length of sliding window is enough
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;
}
}
边栏推荐
- 新品大揭秘!雅迪冠能 3 多元产品矩阵,满足全球用户出行需求
- Uncover the secrets of new products! Yadi Guanneng 3 multi product matrix to meet the travel needs of global users
- 2022年已经过去一半了,是不是很突然呢?
- SQLAchemy 常用操作
- Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
- 商城小程序源码开源版-可二开
- 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
- Sqlachemy common operations
- What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
- 【邂逅Django】——(二)数据库配置
猜你喜欢
12款大家都在用的產品管理平臺
数据库的增删改查问题
[MPC] ① quadratic programming problem matlab solver quadprog
Detailed explanation of linear regression in machine learning
A new round of popularity of digital collections opens
Venv: directory structure of venv
Error: missing revert data in call exception
SQL optimization - in and not in, exist
Suggest collecting | what to do when encountering slow SQL on opengauss?
Who's still buying three squirrels
随机推荐
【邂逅Django】——(二)数据库配置
Button button clear border
A new round of popularity of digital collections opens
Kotlin 协程调度切换线程是时候解开真相了
内存泄漏定位工具之 valgrind 使用
Wireshark TS | confusion between fast retransmission and out of sequence
Have you learned the necessary global exception handler for the project
Detailed explanation of linear regression in machine learning
2022年现在在网上开通股票账户安全吗?会不会有什么危险?
C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列
[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
[matytype] insert MathType inter line and intra line formulas in CSDN blog
CRC 校验
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
[encounter Django] - (II) database configuration
PHP realizes lottery function
NC | 肠道细胞和乳酸菌共同作用来防止念珠菌感染
Kotlin coprocessor scheduling switch threads it's time to unravel the truth
Project0:小游戏
How do clients request databases?