File tree Expand file tree Collapse file tree 3 files changed +117
-0
lines changed
solution/0000-0099/0076.Minimum Window Substring Expand file tree Collapse file tree 3 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 40
40
41
41
## 解法
42
42
43
+ 滑动窗口
44
+
43
45
<!-- 这里可写通用的实现逻辑 -->
44
46
45
47
<!-- tabs:start -->
60
62
61
63
```
62
64
65
+ ### ** TypeScript**
66
+
67
+ ``` ts
68
+ function minWindow(s : string , t : string ): string {
69
+ let n1 = s .length , n2 = t .length ;
70
+ if (n1 < n2 ) return ' ' ;
71
+ let need = new Array (128 ).fill (0 );
72
+ let window = new Array (128 ).fill (0 );
73
+ for (let i = 0 ; i < n2 ; ++ i ) {
74
+ ++ need [t .charCodeAt (i )];
75
+ }
76
+
77
+ let left = 0 , right = 0 ;
78
+ let res = ' ' ;
79
+ let count = 0 ;
80
+ let min = n1 + 1 ;
81
+ while (right < n1 ) {
82
+ let cur = s .charCodeAt (right );
83
+ ++ window [cur ];
84
+ if (need [cur ] > 0 && need [cur ] >= window [cur ]) {
85
+ ++ count ;
86
+ }
87
+ while (count == n2 ) {
88
+ cur = s .charCodeAt (left );
89
+ if (need [cur ] > 0 && need [cur ] >= window [cur ]) {
90
+ -- count ;
91
+ }
92
+ if (right - left + 1 < min ) {
93
+ min = right - left + 1 ;
94
+ res = s .slice (left , right + 1 );
95
+ }
96
+ -- window [cur ];
97
+ ++ left ;
98
+ }
99
+ ++ right ;
100
+ }
101
+ return res ;
102
+ };
103
+ ```
104
+
63
105
### ** ...**
64
106
65
107
```
Original file line number Diff line number Diff line change 43
43
44
44
```
45
45
46
+ ### ** TypeScript**
47
+
48
+ ``` ts
49
+ function minWindow(s : string , t : string ): string {
50
+ let n1 = s .length , n2 = t .length ;
51
+ if (n1 < n2 ) return ' ' ;
52
+ let need = new Array (128 ).fill (0 );
53
+ let window = new Array (128 ).fill (0 );
54
+ for (let i = 0 ; i < n2 ; ++ i ) {
55
+ ++ need [t .charCodeAt (i )];
56
+ }
57
+
58
+ let left = 0 , right = 0 ;
59
+ let res = ' ' ;
60
+ let count = 0 ;
61
+ let min = n1 + 1 ;
62
+ while (right < n1 ) {
63
+ let cur = s .charCodeAt (right );
64
+ ++ window [cur ];
65
+ if (need [cur ] > 0 && need [cur ] >= window [cur ]) {
66
+ ++ count ;
67
+ }
68
+ while (count == n2 ) {
69
+ cur = s .charCodeAt (left );
70
+ if (need [cur ] > 0 && need [cur ] >= window [cur ]) {
71
+ -- count ;
72
+ }
73
+ if (right - left + 1 < min ) {
74
+ min = right - left + 1 ;
75
+ res = s .slice (left , right + 1 );
76
+ }
77
+ -- window [cur ];
78
+ ++ left ;
79
+ }
80
+ ++ right ;
81
+ }
82
+ return res ;
83
+ };
84
+ ```
85
+
46
86
### ** ...**
47
87
48
88
```
Original file line number Diff line number Diff line change
1
+ function minWindow ( s : string , t : string ) : string {
2
+ let n1 = s . length , n2 = t . length ;
3
+ if ( n1 < n2 ) return '' ;
4
+ let need = new Array ( 128 ) . fill ( 0 ) ;
5
+ let window = new Array ( 128 ) . fill ( 0 ) ;
6
+ for ( let i = 0 ; i < n2 ; ++ i ) {
7
+ ++ need [ t . charCodeAt ( i ) ] ;
8
+ }
9
+
10
+ let left = 0 , right = 0 ;
11
+ let res = '' ;
12
+ let count = 0 ;
13
+ let min = n1 + 1 ;
14
+ while ( right < n1 ) {
15
+ let cur = s . charCodeAt ( right ) ;
16
+ ++ window [ cur ] ;
17
+ if ( need [ cur ] > 0 && need [ cur ] >= window [ cur ] ) {
18
+ ++ count ;
19
+ }
20
+ while ( count == n2 ) {
21
+ cur = s . charCodeAt ( left ) ;
22
+ if ( need [ cur ] > 0 && need [ cur ] >= window [ cur ] ) {
23
+ -- count ;
24
+ }
25
+ if ( right - left + 1 < min ) {
26
+ min = right - left + 1 ;
27
+ res = s . slice ( left , right + 1 ) ;
28
+ }
29
+ -- window [ cur ] ;
30
+ ++ left ;
31
+ }
32
+ ++ right ;
33
+ }
34
+ return res ;
35
+ } ;
You can’t perform that action at this time.
0 commit comments