-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinimum_Window_Substring.cpp
79 lines (70 loc) · 2.54 KB
/
Minimum_Window_Substring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Given a string source and a string target, find the minimum window in source which will contain all the characters in target.
Note
If there is no such window in source that covers all characters in target, return the emtpy string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in source.
Example
source = "ADOBECODEBANC" target = "ABC" Minimum window is "BANC".
Challenge Expand
Can you do it in time complexity O(n) ?
Clarification Expand
Should the characters in minimum window has the same order in target?
- Not necessary.
*/
#include <string>
#include <map>
using namespace std;
class Solution {
public:
/**
* @param source: A string
* @param target: A string
* @return: A string denote the minimum window
* Return "" if there is no such a string
*/
string minWindow(string& source, string& target) {
// write your code here
string S = source;
string T = target;
map<char, int> T_has;
map<char, int> now_we_has;
// init hashtable
for (int i = 0; i < T.length(); i++) {
if (T_has.find(T[i]) != T_has.end()) {
T_has[T[i]]++;
} else {
T_has[T[i]] = 1;
now_we_has[T[i]] = 0;
}
}
// for loop
int count = 0;
int minWinBegin = -1;
int minWinEnd = S.length();
int begin = 0;
for (int end = 0; end < S.length(); end++) {
char c = S[end];
if (T_has.find(c) != T_has.end()) {
now_we_has[c]++;
if (now_we_has[c] <= T_has[c]) {
count++;
}
// find one valid substr
if (count == T.length()) {
//while (1) S[begin] is useless char, (2) now_we_has[S[begin]]> T_has[S[begin]]
while (T_has.find(S[begin]) == T_has.end() || now_we_has[S[begin]] > T_has[S[begin]]) {
if ((T_has.find(S[begin]) != T_has.end() && now_we_has[S[begin]] > T_has[S[begin]])) {
now_we_has[S[begin]]--;
}
begin++;
}
if (end - begin < minWinEnd - minWinBegin) {
minWinEnd = end;
minWinBegin = begin;
}
}
}
}
return minWinBegin == -1 ? "" : S.substr(minWinBegin, minWinEnd - minWinBegin + 1);
}
};