当前位置:网站首页>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;
}
}
边栏推荐
- [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
- 数据库的增删改查问题
- Ask everyone in the group about the fact that the logminer scheme of flick Oracle CDC has been used to run stably in production
- 大佬们 有没有搞过sink分流写入clickhouse 或者其他数据库的操作。
- Design and practice of new generation cloud native database
- Guys, how to export iceberg data to MySQL? What tools are there? Neither sqoop nor dataX
- SQL server2014 failed to delete the database, with an error offset of 0x0000
- bash: ln: command not found
- .NET 5.0+ 无需依赖第三方 原生实现定时任务
- leetcode:111. Minimum depth of binary tree
猜你喜欢
![[matytype] insert MathType inter line and intra line formulas in CSDN blog](/img/ff/871a3f06f898ed107a2a974d2c7bc4.png)
[matytype] insert MathType inter line and intra line formulas in CSDN blog

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;

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

Half of 2022 has passed, isn't it sudden?

《百年巨匠》数字藏品中奖名单公布

mysql如何把 一个数据库中的表数据 复制到 另一个数据库中(两个数据库不在同一个数据库链接下)

推荐一款 JSON 可视化工具神器!

SQL server2014 failed to delete the database, with an error offset of 0x0000

PHP有哪些优势和劣势

Error: missing revert data in call exception
随机推荐
SQL Server列一相同的情况下,如何取列二的最大值,并重新生成表
[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
Valgrind usage of memory leak locating tool
Button button clear border
Uncover the secrets of new products! Yadi Guanneng 3 multi product matrix to meet the travel needs of global users
《百年巨匠》数字藏品中奖名单公布
12.Gateway新一代网关
12款大家都在用的产品管理平台
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
JS基础--数据类型
Which securities company has a low, safe and reliable Commission for stock trading and account opening
个人商城二开逍遥B2C商城系统源码-可商用版/拼团拼购优惠折扣秒杀源码
亿学学堂帮个人开的证券账户安全吗?是不是有套路
Rising Stars in Plant Sciences (RSPS2022) Finalist科学演讲会(6.30晚9点)
What legal risks and qualifications should be paid attention to when building a digital collection platform?
php 实现抽奖功能
想开个户,在网上开华泰证券的户安全吗?
Rising stars in Plant Sciences (rsps2022) final Science Lecture (6.30 pm)
mysql cdc能把能把op字段拿出来吗
[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming