当前位置:网站首页>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;
}
}
边栏推荐
- [matytype] insert MathType inter line and intra line formulas in CSDN blog
- [paper reading] trajectory guided control prediction for end to end autonomous driving: a simple yet strong Ba
- 新品大揭秘!雅迪冠能 3 多元产品矩阵,满足全球用户出行需求
- Addition, deletion, modification and query of database
- 爬虫(2) - Requests(1) | Requests模块的深度解析
- 数字藏品平台搭建需要注意哪些法律风险及资质?
- Can MySQL CDC take out the op field
- Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
- How do clients request databases?
- Can I choose to open an account on CICC securities? Is it safe?
猜你喜欢

数据库的增删改查问题

12款大家都在用的產品管理平臺

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

【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Ba

Wireshark TS | 快速重传和乱序之混淆

The Lantern Festival is held on the fifteenth day of the first month, and the Lantern Festival begins to celebrate the reunion

106. 从中序与后序遍历序列构造二叉树

bash: ln: command not found

Kotlin 协程调度切换线程是时候解开真相了

价值1000毕业设计校园信息发布平台网站源码
随机推荐
大佬们 有没有搞过sink分流写入clickhouse 或者其他数据库的操作。
[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
商城小程序源码开源版-可二开
CRC 校驗
CRC check
推荐一款 JSON 可视化工具神器!
flutter path_provider: ^2.0.10可以获取临时目录
Error: missing revert data in call exception
106. 从中序与后序遍历序列构造二叉树
. Net 5.0+ does not need to rely on third-party native implementation of scheduled tasks
Crawler (2) - requests (1) | deep parsing of requests module
12.Gateway新一代网关
【MPC】①二次规划问题MATLAB求解器quadprog
Is it safe to do fund fixed investment on CICC securities?
How to solve the problem of SQL?
2022年已经过去一半了,是不是很突然呢?
MySQL common commands
The list of winners of the digital collection of "century master" was announced
SQL server2014 failed to delete the database, with an error offset of 0x0000
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc