-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathMinimum Window Substring.cpp
109 lines (83 loc) · 3.23 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
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
/* Scroll below to see JAVA code also */
/*
MY YOUTUBE VIDEO ON THIS Qn : https://www.youtube.com/watch?v=3Bp3OVD1EGc
Company Tags : Google, Amazon, Microsoft, Codenation, FactSet, , Atlassian, MakeMyTrip, Streamoid Technologies, Media.net, Airbnb
Leetcode Link : https://leetcode.com/problems/minimum-window-substring/
*/
/***************************************************************** C++ *****************************************************************/
//T.C : O(m+n) where m = length of s and n = length of t
//S.C : O(n)
class Solution {
public:
string minWindow(string s, string t) {
int n = s.length();
map<char, int> mp;
for(char &ch : t) {
mp[ch]++;
}
int requiredCount = t.length();
int i = 0, j = 0;
int minStart = 0;
int minWindow = INT_MAX;
while(j < n) {
char ch_j = s[j];
if(mp[ch_j] > 0)
requiredCount--;
mp[ch_j]--;
while(requiredCount == 0) { //try to shrink the window
if(minWindow > j-i+1) {
minWindow = j-i+1;
minStart = i;
}
char ch_i = s[i];
mp[ch_i]++;
if(mp[ch_i] > 0)
requiredCount++;
i++;
}
j++; //Don't ever forget this :-)
}
return minWindow == INT_MAX ? "" : s.substr(minStart, minWindow);
}
};
/***************************************************************** JAVA *****************************************************************/
//T.C : O(m+n) where m = length of s and n = length of t
//S.C : O(n)
public class Solution {
public String minWindow(String s, String t) {
int n = s.length();
if (t.length() > n)
return "";
Map<Character, Integer> mp = new HashMap<>();
// store karliya
for (char ch : t.toCharArray())
mp.put(ch, mp.getOrDefault(ch, 0) + 1);
int requiredCount = t.length();
int i = 0, j = 0;
int minWindowSize = Integer.MAX_VALUE;
int start_i = 0;
// story starts
while (j < n) {
char ch = s.charAt(j);
if (mp.containsKey(ch) && mp.get(ch) > 0)
requiredCount--;
mp.put(ch, mp.getOrDefault(ch, 0) - 1);
while (requiredCount == 0) {
// start shrinking the window
int currWindowSize = j - i + 1;
if (minWindowSize > currWindowSize) {
minWindowSize = currWindowSize;
start_i = i;
}
char startChar = s.charAt(i);
mp.put(startChar, mp.getOrDefault(startChar, 0) + 1);
if (mp.containsKey(startChar) && mp.get(startChar) > 0) {
requiredCount++;
}
i++;
}
j++;
}
return minWindowSize == Integer.MAX_VALUE ? "" : s.substring(start_i, start_i + minWindowSize);
}
}