1
+ //给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。
2
+ //
3
+ //
4
+ //
5
+ // 注意:
6
+ //
7
+ //
8
+ // 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
9
+ // 如果 s 中存在这样的子串,我们保证它是唯一的答案。
10
+ //
11
+ //
12
+ //
13
+ //
14
+ // 示例 1:
15
+ //
16
+ //
17
+ //输入:s = "ADOBECODEBANC", t = "ABC"
18
+ //输出:"BANC"
19
+ //
20
+ //
21
+ // 示例 2:
22
+ //
23
+ //
24
+ //输入:s = "a", t = "a"
25
+ //输出:"a"
26
+ //
27
+ //
28
+ // 示例 3:
29
+ //
30
+ //
31
+ //输入: s = "a", t = "aa"
32
+ //输出: ""
33
+ //解释: t 中两个字符 'a' 均应包含在 s 的子串中,
34
+ //因此没有符合条件的子字符串,返回空字符串。
35
+ //
36
+ //
37
+ //
38
+ // 提示:
39
+ //
40
+ //
41
+ // 1 <= s.length, t.length <= 105
42
+ // s 和 t 由英文字母组成
43
+ //
44
+ //
45
+ //
46
+ //进阶:你能设计一个在 o(n) 时间内解决此问题的算法吗? Related Topics 哈希表 字符串 滑动窗口
47
+ // 👍 1269 👎 0
48
+
49
+
50
+ package com .jacobs .basic .leetcode .editor .cn ;
51
+
52
+ public class MinimumWindowSubstring {
53
+ public static void main (String [] args ) {
54
+ Solution solution = new MinimumWindowSubstring ().new Solution ();
55
+ System .out .println (solution .minWindow ("ADOBECODEBANC" , "ABC" ));
56
+ }
57
+
58
+ //leetcode submit region begin(Prohibit modification and deletion)
59
+ class Solution {
60
+ public String minWindow (String s , String t ) {
61
+ if (s == null || s .length () == 0 || t == null || t .length () == 0 ) {
62
+ return "" ;
63
+ }
64
+ int [] rawCharMap = new int [256 ];
65
+ int [] repCharMap = new int [256 ];
66
+ char [] rawChars = s .toCharArray ();
67
+ char [] repChars = t .toCharArray ();
68
+ int repLen = repChars .length ;
69
+ for (int i = 0 ; i < repChars .length ; i ++) {
70
+ repCharMap [repChars [i ]] += 1 ;
71
+ }
72
+ int minLeft = 0 ;
73
+ int minRight = 0 ;
74
+ int minLen = Integer .MAX_VALUE ;
75
+ int left = 0 ;
76
+ int right = 0 ;
77
+ int matchedLen = 0 ;
78
+ while (left <= right && right < rawChars .length ) {
79
+ char tmpChar = rawChars [right ];
80
+ if (repCharMap [tmpChar ] <= 0 ) {
81
+ //刚开始left也可以跟着平移,在没找到第一个matched之前
82
+ if (repCharMap [rawChars [left ]] <= 0 ) {
83
+ left ++;
84
+ }
85
+ right ++;
86
+ continue ;
87
+ }
88
+ if (repCharMap [tmpChar ] > 0 ) {
89
+ if (rawCharMap [tmpChar ] < repCharMap [tmpChar ]) {
90
+ matchedLen ++;
91
+ }
92
+ rawCharMap [tmpChar ] += 1 ;
93
+ }
94
+ // 如果还没全部match上的话,则right继续往右扩
95
+ if (matchedLen < repLen ) {
96
+ right ++;
97
+ continue ;
98
+ }
99
+ while (left < right ) {
100
+ tmpChar = rawChars [left ];
101
+ // 中间可能有根本不属于的case,需要跳过
102
+ if (rawCharMap [tmpChar ] <= 0 ) {
103
+ left ++;
104
+ } else if (rawCharMap [tmpChar ] > repCharMap [tmpChar ] && matchedLen >= repLen ) {
105
+ left ++;
106
+ rawCharMap [tmpChar ] -= 1 ;
107
+ } else {
108
+ // 如果刚好在左边界没有多余的,则停止left缩小
109
+ break ;
110
+ }
111
+ }
112
+ if (right - left < minLen && matchedLen >= repLen ) {
113
+ minLen = right - left ;
114
+ minLeft = left ;
115
+ minRight = right ;
116
+ }
117
+ right ++;
118
+ }
119
+ if (matchedLen < repLen ) {
120
+ return "" ;
121
+ }
122
+ return s .substring (minLeft , minRight + 1 );
123
+ }
124
+ }
125
+ }
126
+ //leetcode submit region end(Prohibit modification and deletion)
0 commit comments