-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76_Minimum_Window_Substring.cpp
More file actions
191 lines (179 loc) · 3.78 KB
/
Copy path76_Minimum_Window_Substring.cpp
File metadata and controls
191 lines (179 loc) · 3.78 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
#define ll long long
#define ull unsigned long long
#define readi(x) int x; cin >> x
#define readll(x) ll x; cin >> x
#define reads(s) string s; cin >> s
#define rep(a, b) for (ll i = a; i < b; ++i)
#define repi(i, a, b) for (ll i = a; i < b; ++i)
#define repd(i, a, b) for (ll i = a; i > b; --i)
#define all(x) (x).begin(), (x).end()
#define init_arr readll(n); vll arr(n); rep(0, n) cin >> arr[i];
#define vprint(i) for (auto &j : i) cout << j << ' '; nl
#define mpprint(mp) for (auto &i : mp) { cout << i.ff << ' ' << i.ss << '\n'; }
#define vvprint(arr) for (auto &i : arr) { vprint(i) }
#define vvcin(n, m, arr) repi(i, 0, n) repi(j, 0, m) cin >> arr[i][j];
#define vcin(n, arr) rep(0, n) cin >> arr[i];
#define umcll unordered_map<char, ll >
#define umsll unordered_map<string, ll >
#define umll unordered_map<ll, ll >
#define print(num) cout << num << '\n';
#define mpcll map<char, ll >
#define mpsll map<string, ll >
#define mpll map<ll, ll >
#define nl cout << '\n';
#define inf (1LL << 60)
#define vll vector<ll >
#define vi vector<int >
#define vii vector<vi >
#define vs vector<string >
#define vss vector<vs >
#define vb vector<bool >
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mod 1e9 + 7
#define ff first
#define ss second
#define N 1e6
// TC : O(n+m)
// SC : O(n)
class Solution {
public:
string minWindow(string s, string t) {
string res = "";
unordered_map<char, int> map1, map2;
int m = t.size(), n = s.size();
if (m > n)
return "";
for (int i = 0; i < m; ++i)
{
map2[t[i]]++;
}
int cnt = 0, i = 0, j = 0;
deque<char> temp1, temp2;
while (true)
{
bool f1 = true, f2 = true;
while (i < n && cnt < m)
{
map1[s[i]]++;
temp2.push_back(s[i]);
if (map1[s[i]] <= map2[s[i]])
{
cnt++;
}
i++;
f1 = false;
}
while (j < i && cnt == m)
{
if (temp1.empty() || temp1.size() > temp2.size())
{
temp1 = temp2;
}
if (map1[s[j]] == 1)
{
map1.erase(s[j]);
}
else
{
map1[s[j]]--;
}
if (map1[s[j]] < map2[s[j]])
{
cnt--;
}
temp2.pop_front();
j++;
f2 = false;
}
if (f1 && f2)
{
break;
}
}
while (!temp1.empty())
{
res.push_back(temp1.front());
temp1.pop_front();
}
return res;
}
};
class Solution1 {
public:
string minWindow(string s, string t) {
unordered_map<char, int> mp;
int tn = t.size();
for (int i = 0; i < tn; i++) {
mp[t[i]]++;
}
int charCnt = mp.size();
int i = 0, j = 0;
int sn = s.size();
int len = INT_MAX;
int strtIdx = 0;
string result = "";
while (j < sn) {
//finding the character in map.
if (mp.find(s[j]) != mp.end()) {
mp[s[j]]--;
if (mp[s[j]] == 0)
charCnt--;
}
//character count > 0 then slide the winndow.
if (charCnt > 0)
j++;
else if (charCnt == 0) {
if (j - i + 1 < len) {
len = min(len, j - i + 1);
strtIdx = i;
result = s.substr(strtIdx, len);
}
//minimize the window if possible
while (charCnt == 0) {
if (mp.find(s[i]) == mp.end()) {
i++;
if (j - i + 1 < len) {
len = min(len, j - i + 1);
strtIdx = i;
result = s.substr(strtIdx, len);
}
}
else {
mp[s[i]]++;
if (mp[s[i]] == 1) {
i++;
charCnt++;
}
else {
i++;
if (j - i + 1 < len) {
len = min(len, j - i + 1);
strtIdx = i;
result = s.substr(strtIdx, len);
}
}
}
}
j++;
}
}
return (len == INT_MAX ? "" : result);
}
};
int main() {
fast_io;
ll t = 1;
// cin >> t;
while (t--) {
Solution ob;
reads(s);
reads(t);
cout << ob.minWindow(s, t) << '\n';
}
return 0;
}