-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
144 lines (132 loc) · 3.45 KB
/
main.cpp
File metadata and controls
144 lines (132 loc) · 3.45 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
// Source: https://leetcode.com/problems/minimum-window-substring
// Title: Minimum Window Substring
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given two strings `s` and `t` of lengths `m` and `n` respectively, return the **minimum window** <button type="button" aria-haspopup="dialog" aria-expanded="false" aria-controls="radix-:rs:" data-state="closed" class="">**substring**</button> 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 `""`.
//
// The testcases will be generated such that the answer is **unique**.
//
// **Example 1:**
//
// ```
// Input: s = "ADOBECODEBANC", t = "ABC"
// Output: "BANC"
// Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
// ```
//
// **Example 2:**
//
// ```
// Input: s = "a", t = "a"
// Output: "a"
// Explanation: The entire string s is the minimum window.
// ```
//
// **Example 3:**
//
// ```
// Input: s = "a", t = "aa"
// Output: ""
// Explanation: Both 'a's from t must be included in the window.
// Since the largest window of s only has one 'a', return empty string.
// ```
//
// **Constraints:**
//
// - `m == s.length`
// - `n == t.length`
// - `1 <= m, n <= 10^5`
// - `s` and `t` consist of uppercase and lowercase English letters.
//
// **Follow up:** Could you find an algorithm that runs in `O(m + n)` time?
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// Use two pointer
class Solution {
public:
string minWindow(string s, string t) {
int n = s.size();
unordered_map<char, int> tCounter;
unordered_map<char, int> sCounter;
// Count chars
for (auto ch : t) {
tCounter[ch]++;
}
// Compare counter
auto check = [&]() -> bool {
for (auto [ch, tCount] : tCounter) {
if (sCounter[ch] < tCount) {
return false;
}
}
return true;
};
auto ansSize = n + 1;
auto ansI = 0;
int i = 0;
int j = 0;
while (j <= n) {
int size = j - i;
if (check()) {
if (ansSize > size) {
ansSize = size;
ansI = i;
}
sCounter[s[i]]--;
i++;
} else {
sCounter[s[j]]++;
j++;
}
}
if (ansSize == n + 1) {
return "";
}
return s.substr(ansI, ansSize);
}
};
// Use two pointer
class Solution2 {
public:
string minWindow(string s, string t) {
int n = s.size();
// Count chars
vector<int> counter(128); // remaining char count need
auto count = 0; // number of positive count
for (auto ch : t) {
counter[ch]++;
}
for (auto c : counter) {
if (c > 0) count++;
}
auto ansSize = n + 1;
auto ansI = 0;
int i = 0;
int j = 0;
while (j <= n) {
int size = j - i;
if (count == 0) {
if (ansSize > size) {
ansSize = size;
ansI = i;
}
if (counter[s[i]] == 0) count++;
counter[s[i]]++;
i++;
} else {
counter[s[j]]--;
if (counter[s[j]] == 0) count--;
j++;
}
}
if (ansSize == n + 1) {
return "";
}
return s.substr(ansI, ansSize);
}
};