Advertisement
pasholnahuy

minimal substring including all letters from string

Mar 14th, 2024
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <unordered_set>
  4.  
  5. using namespace std;
  6. int main() {
  7.     string s;
  8.     cin >> s;
  9.     unordered_set<char> alf;
  10.     for (auto el: s){
  11.         alf.insert(el);
  12.     }
  13.     unordered_map<char, int> cur_alf;
  14.     int l = 0;
  15.     int r = 0;
  16.     int ans = s.size();
  17.     while (r != s.size()){
  18.         while (r < s.size() && cur_alf.size() < alf.size()){
  19.             ++cur_alf[s[r]];
  20.             ++r;
  21.         }
  22.         while (cur_alf.size() == alf.size()){
  23.             --cur_alf[s[l]];
  24.             if (cur_alf[s[l]] == 0){
  25.                 cur_alf.erase(s[l]);
  26.             }
  27.             ++l;
  28.         }
  29.         ans = min(ans, r - l + 1);
  30.     }
  31.     cout << ans;
  32. }
  33. //abbac
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement