#include<unordered_map> stringminWindow(string s, string t){ int start = 0, len = s.length()+1;//截取的开始位置与长度 int left = 0, right = 0; unordered_map<char, int> window;//由s获得的字符及次数 unordered_map<char, int> needs;//由t获得的字符及次数 for (char c : t)needs[c]++; int match = 0; while (right < s.length()) { char c = s[right]; if (needs.count(c)) {//命中 window[c]++; if (window[c] == needs[c]) match++; } right++; while (match == needs.size()) {//更新截取结果,当始终包含t的字符时,left右移 if ((right - left) < len) { start = left; len = right - left; } char k = s[left]; if (needs.count(k)) {//可能left移到了非关键字母上 window[k]--; if(window[k] < needs[k])//可能有重复积累的 match--; } left++; } } if (len > s.length())return""; else return s.substr(start, len); }