-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathMinimum_Window_substring.cpp
More file actions
70 lines (64 loc) · 1.63 KB
/
Minimum_Window_substring.cpp
File metadata and controls
70 lines (64 loc) · 1.63 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
// Problem Statement: Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string.
// Examples:
// Example 1:
// Input: s = "ADOBECODEBANC", t = "ABC"
// Output: 4
// Explanation: BANC has all required characters
// Example 2:
// Input: s = "ABBXC", t = "BXC"
// Output: 3
// Explanation: BXC has all required characters
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
bool check(map < char, int > mpt, map < char, int > mps) {
for (auto i: mpt) {
if (mps.find(i.first) == mps.end()) return false;
if (mps[i.first] < i.second) return false;
}
return true;
}
string makeString(int i, int j, string s) {
string str = "";
while (i <= j) {
str += s[i];
i++;
}
return str;
}
string minWindow(string s, string t) {
map < char, int > mpt;
map < char, int > mps;
for (int i = 0; i < t.length(); i++) {
mpt[t[i]]++;
}
int i = 0, j = 0;
int ans = INT_MAX;
string str = "";
while (j < s.length()) {
if (mpt.find(s[j]) != mpt.end()) {
if (mps.size() == 0) i = j;
mps[s[j]]++;
}
if (j - i + 1 >= t.length()) {
bool f = check(mpt, mps);
if (f) {
mps.clear();
if (ans > j - i + 1) {
str = makeString(i, j, s);
ans = j - i + 1;
}
j = i + 1;
i = j;
continue;
}
}
j++;
}
return str;
}
int main() {
string s = "adobecodebanc", t = "abc";
cout << "The minimum window is: " << minWindow(s, t);
return 0;
}