File tree Expand file tree Collapse file tree 4 files changed +137
-72
lines changed Expand file tree Collapse file tree 4 files changed +137
-72
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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" ) )
Original file line number Diff line number Diff line change 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" ) )
You can’t perform that action at this time.
0 commit comments