LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
ADOBECODEBANC => 不包含 ABC,右移 r

l
r

ADOBECODEBANC => 包含 ABC,右移 l
    
l    r

ADOBECODEBANC => 不包含 ABC,右移 r
    
 l   r

ADOBECODEBANC => 包含 ABC,右移 l
         
 l        r

ADOBECODEBANC => 包含 ABC,右移 l
         
  l       r

ADOBECODEBANC => 包含 ABC,右移 l
         
   l      r

ADOBECODEBANC => 包含 ABC,右移 l
         
    l     r

ADOBECODEBANC => 包含 ABC,右移 l
         
     l    r


ADOBECODEBANC => 不包含 ABC,右移 r
         
      l   r

ADOBECODEBANC => 包含 ABC,右移 l
           
      l     r

ADOBECODEBANC => 包含 ABC,右移 l
           
         l  r

ADOBECODEBANC => 不包含 ABC,右移 r,超出边界,结束
           
          l r

整个过程中满足条件的最短子串 BANC 即为结果
class Solution {
 public:
  string minWindow(string s, string t) {
    unordered_map<char, int> m;
    for (auto& x : t) {
      ++m[x];  // 记录 t 中所有字符的出现次数
    }
    int minLen = INT_MAX;  // 最小窗口长度
    int start = 0;         // 窗口长度最小时的起点
    int l = 0;
    int cnt = 0;  // 窗口包含的目标字符数
    for (int r = 0; r < size(s); ++r) {
      if (--m[s[r]] >= 0) {
        ++cnt;
      }
      while (cnt == size(t)) {  // 窗口满足条件
        if (r - l + 1 < minLen) {
          minLen = r - l + 1;
          start = l;
        }
        if (++m[s[l]] > 0) {
          --cnt;
        }
        ++l;
      }
    }
    return minLen == INT_MAX ? "" : s.substr(start, minLen);
  }
};