var findAnagrams = function(s, p) {
let n=s.length
let m=p.length
let number=new Array()
let a='a'.charCodeAt()
//窗口首尾指针
let d=0
let k=m-1
while(s[k]){
let arr=new Array(26).fill(0)
//统计p中的字符和频率
for(let i of p){
arr[i.charCodeAt()-a]++
}
//截取当前窗口内的字符
str=s.slice(d,k+1)
for(let x of str){
arr[x.charCodeAt()-a]--
}
let b=0 // 标识
//判断当前窗口的字符串是否是p的异位词
for(let y=0;y<26;y++){
if(arr[y]!==0){
b=1
}
}
if(b===0){
number.push(d)
}
d++
k++
b=0
}
return number
};
力扣438.找到字符串中所有字母异位词(JavaScript)
最新推荐文章于 2023-11-21 22:45:00 发布