共同点
- 记录窗口中出现的字符及次数 casement
- 记录题目要求的字符及出现次数 need
- 判断窗口中符合要求的字符出现总数 valid
- 对新增和移出字符在窗口中出现次数的处理,对符合要求字符总数的更改
题目差异
- 收缩窗口的条件
- 记录答案的条件
五道题目
76.最小重复子串
var minWindow = function(s, t) {
let need = new Map()
let casement = new Map()
let valid = 0
let [left,right] = [0,0]
let [start,length] = [0,Number.MAX_SAFE_INTEGER]
for(let str of t){
need.set(str,need.get(str) ? need.get(str) + 1 : 1)
}
while(right < s.length){
let newChar = s[right]
casement.set(newChar,casement.get(newChar) ? casement.get(newChar)+1 : 1)
if(casement.get(newChar) <= need.get(newChar)){
valid++
}
right++
while(valid == t.length){
if(right - left < length){
start = left
length = right - left
}
let moveChar = s[left]
if(need.get(moveChar)){
if(casement.get(moveChar) == need.get(moveChar)){
valid--
}
casement.set(moveChar,casement.get(moveChar)-1)
}
left++
}
}
return length == Number.MAX_SAFE_INTEGER ? "" : s.substr(start,length)
};
567.字符串的排列
var checkInclusion = function(s1, s2) {
let casement = new Map()
let need = new Map()
for(let char of s1){
need.set(char,need.get(char) ? need.get(char)+1 : 1)
}
let [left,right] = [0,0]
let valid = 0
while(right < s2.length){
let newChar = s2[right]
right++
casement.set(newChar,casement.get(newChar) ? casement.get(newChar)+1 : 1)
if(casement.get(newChar) <= need.get(newChar)){
valid++
}
while(right - left >= s1.length){
if(valid == s1.length) return true
let moveChar = s2[left]
left++
casement.set(moveChar,casement.get(moveChar)-1)
if(casement.get(moveChar) < need.get(moveChar)){
valid--
}
console.log(moveChar,)
}
}
return false
};
438. 找到字符串中所有字母异位词
var findAnagrams = function(s, p) {
let casement = new Map()
let need = new Map()
let ans = []
let [left,right] = [0,0]
let valid = 0
for(let char of p){
need.set(char,need.get(char) ? need.get(char) +1 : 1)
}
while(right < s.length){
let newChar = s[right]
right++
casement.set(newChar,casement.get(newChar) ? casement.get(newChar)+1 : 1)
if(need.get(newChar)){
if(casement.get(newChar) <= need.get(newChar)){
valid++
}
}
while(valid == p.length){
if(right - left == p.length) ans.push(left)
let moveChar = s[left]
left++
casement.set(moveChar,casement.get(moveChar) ? casement.get(moveChar)-1 : 1)
if(casement.get(moveChar) < need.get(moveChar)){
valid--
}
}
}
return ans
};
3. 无重复字符的最长子串
var lengthOfLongestSubstring = function(s) {
let casement = new Map()
let [left,right] = [0,0]
let ans = 0
while(right < s.length){
let newChar = s[right]
casement.set(newChar,casement.get(newChar) ? casement.get(newChar)+1 : 1)
right++
while(casement.get(newChar) > 1){
let moveChar = s[left]
casement.set(moveChar,casement.get(moveChar)-1)
left++
}
ans = right-left > ans ? right-left : ans
}
return ans
};
var lengthOfLongestSubstring = function(s) {
let str = '',
ans = 0
for(let i = 0;i < s.length;i++){
if(str.indexOf(s[i]) != -1){
str = str.slice(str.indexOf(s[i])+1) + s[i]
}else{
str += s[i]
}
ans = Math.max(ans,str.length)
}
return ans
};
187. 重复的DNA序列
var findRepeatedDnaSequences = function(s) {
let casement = new Set()
let ans = []
let str = ''
let [left,right] = [0,0]
while(right < s.length){
str += s[right]
right++
if(str.length == 10){
if(casement.has(str)){
ans.push(str)
}else{
casement.add(str)
}
str = str.replace(s[left],'')
left++
}
}
let newSet = new Set()
ans.map(item=>{
newSet.add(item)
})
return Array.from(newSet)
};