Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string minWindow(string s, string t) {
- string ans;
- int n = s.size();
- int minLen = INT_MAX;
- int index = -1;
- if (s.size() < t.size()) return ans;
- unordered_map<char, int> hashT;
- unordered_map<char, int> hashS;
- for (char c : t) ++hashT[c];
- for (int i = 0, j = 0; j < n; ++j) {
- ++hashS[s[j]];
- while (checkValid(hashS, hashT)) {
- int len = j - i + 1;
- if (len < minLen) {
- minLen = len;
- index = i;
- }
- if (--hashS[s[i]] == 0) hashS.erase(s[i]);
- ++i;
- }
- }
- return minLen == INT_MAX ? "" : s.substr(index, minLen);
- }
- bool checkValid(unordered_map<char, int>& hashS, unordered_map<char, int>& hashT) {
- for (auto item : hashT) {
- if (hashS.find(item.first) == hashS.end()) return false;
- if (hashS[item.first] < item.second) return false;
- }
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment