Skip to content

Commit e3d14da

Browse files
committed
day 15
1 parent 6e43c8a commit e3d14da

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Minimum Window Substring
3+
========================
4+
5+
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 "".
6+
7+
The testcases will be generated such that the answer is unique.
8+
9+
A substring is a contiguous sequence of characters within the string.
10+
11+
Example 1:
12+
Input: s = "ADOBECODEBANC", t = "ABC"
13+
Output: "BANC"
14+
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
15+
16+
Example 2:
17+
Input: s = "a", t = "a"
18+
Output: "a"
19+
Explanation: The entire string s is the minimum window.
20+
21+
Example 3:
22+
Input: s = "a", t = "aa"
23+
Output: ""
24+
Explanation: Both 'a's from t must be included in the window.
25+
Since the largest window of s only has one 'a', return empty string.
26+
27+
Constraints:
28+
m == s.length
29+
n == t.length
30+
1 <= m, n <= 105
31+
s and t consist of uppercase and lowercase English letters.
32+
33+
Follow up: Could you find an algorithm that runs in O(m + n) time?
34+
*/
35+
36+
class Solution
37+
{
38+
public:
39+
bool same(unordered_map<char, int> &sMap, unordered_map<char, int> &tMap)
40+
{
41+
for (auto &i : tMap)
42+
{
43+
if (sMap[i.first] < i.second)
44+
return false;
45+
}
46+
return true;
47+
}
48+
49+
string minWindow(string s, string t)
50+
{
51+
int Size = s.size() + 1;
52+
int i = 0, j = 0;
53+
string ans, curr;
54+
55+
unordered_map<char, int> tMap, sMap;
56+
for (auto &i : t)
57+
tMap[i]++;
58+
59+
while (j < s.size())
60+
{
61+
sMap[s[j]]++;
62+
j++;
63+
64+
while (same(sMap, tMap))
65+
{
66+
if (Size > j - i)
67+
{
68+
Size = j - i;
69+
ans = "";
70+
for (int k = i; k < j; ++k)
71+
ans += s[k];
72+
}
73+
sMap[s[i]]--;
74+
i++;
75+
}
76+
}
77+
78+
return ans;
79+
}
80+
};

Leetcode Daily Challenge/August-2021/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
| 10. | [Flip String to Monotone Increasing](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3876/) | [cpp](./10.%20Flip%20String%20to%20Monotone%20Increasing.cpp) |
1616
| 11. | [Array of Doubled Pairs](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3877/) | [cpp](./11.%20Array%20of%20Doubled%20Pairs.cpp) |
1717
| 12. | [Group Anagrams](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3887/) | [cpp](./12.%20Group%20Anagrams.cpp) |
18-
| 13. | [Set Matrix Zeroes](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3888/) | [cpp](./13.%20Set%20Matrix%20Zeroes.cpp) |
18+
| 13. | [Set Matrix Zeroes](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3888/) | [cpp](./13.%20Set%20Matrix%20Zeroes.cpp) |
19+
| 15. | [Minimum Window Substring](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3891/) | [cpp](./15.%20Minimum%20Window%20Substring.cpp) |
20+
| 16. | [Range Sum Query - Immutable](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3892/) | [cpp](./16.%20Range%20Sum%20Query%20-%20Immutable.cpp) |

0 commit comments

Comments
 (0)