Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7f309d4

Browse files
committed
feat: 最小覆盖子串
1 parent 317df3d commit 7f309d4

4 files changed

+137
-72
lines changed

无重复字符的最长子串(滑动窗口).js

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} p
4+
* @return {number[]}
5+
*/
6+
let findAnagrams = function (s, p) {
7+
let sl = s.length
8+
let pl = p.length
9+
let last = sl - pl + 1
10+
11+
let res = []
12+
for (let i = 0; i < last; i++) {
13+
if (isAnagrams(s, p, i)) {
14+
res.push(i)
15+
}
16+
}
17+
18+
return res
19+
}
20+
21+
let isAnagrams = function (s, p, start) {
22+
let pl = p.length
23+
let end = start + pl
24+
25+
let sub = s.substring(start, end)
26+
let subMap = {}
27+
let pMap = {}
28+
29+
countStr(sub, subMap)
30+
countStr(p, pMap)
31+
32+
let subKeys = Object.keys(subMap)
33+
for (let subKey of subKeys) {
34+
if (!pMap[subKey] || subMap[subKey] !== pMap[subKey]) {
35+
return false
36+
}
37+
}
38+
return true
39+
40+
function countStr(str, map) {
41+
for (let i = 0; i < str.length; i++) {
42+
if (!map[str[i]]) {
43+
map[str[i]] = 1
44+
} else {
45+
map[str[i]]++
46+
}
47+
}
48+
}
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
let lengthOfLongestSubstring = function (str) {
6+
let n = str.length
7+
// 滑动窗口为s[left...right]
8+
let left = 0
9+
let right = -1
10+
let freqMap = {} // 记录当前子串中下标对应的出现频率
11+
let max = 0 // 找到的满足条件子串的最长长度
12+
13+
while (left < n) {
14+
let nextLetter = str[right + 1]
15+
if (!freqMap[nextLetter] && nextLetter !== undefined) {
16+
freqMap[nextLetter] = 1
17+
right++
18+
} else {
19+
freqMap[str[left]] = 0
20+
left++
21+
}
22+
max = Math.max(max, right - left + 1)
23+
}
24+
25+
return max
26+
}
27+
28+
console.log(lengthOfLongestSubstring("pwwkew"))

滑动窗口/最小覆盖子串.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {string}
5+
*/
6+
let minWindow = function (s, t) {
7+
// 先制定目标 根据t字符串统计出每个字符应该出现的个数
8+
let targetMap = makeCountMap(t)
9+
10+
let sl = s.length
11+
let left = 0
12+
let right = -1
13+
let countMap = {}
14+
let min = ""
15+
16+
while (left <= sl && right <= sl) {
17+
let isValid = true
18+
Object.keys(targetMap).forEach((key) => {
19+
let targetCount = targetMap[key]
20+
let count = countMap[key]
21+
if (!count || count < targetCount) {
22+
isValid = false
23+
}
24+
})
25+
26+
if (isValid) {
27+
let currentValidLength = right - left + 1
28+
if (currentValidLength < min.length || min === "") {
29+
min = s.substring(left, right + 1)
30+
}
31+
// 也要把map里对应的项去掉
32+
countMap[s[left]]--
33+
left++
34+
} else {
35+
addCountToMap(countMap, s[right + 1])
36+
right++
37+
}
38+
}
39+
40+
return min
41+
}
42+
43+
function addCountToMap(map, str) {
44+
if (!map[str]) {
45+
map[str] = 1
46+
} else {
47+
map[str]++
48+
}
49+
}
50+
51+
function makeCountMap(strs) {
52+
let map = {}
53+
for (let i = 0; i < strs.length; i++) {
54+
let letter = strs[i]
55+
addCountToMap(map, letter)
56+
}
57+
return map
58+
}
59+
60+
console.log(minWindow("ADOBECODEBANC", "ABC"))

0 commit comments

Comments
 (0)