█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 7020

Problema 0xc9 - Substring más corta con mínimo K caracteres diferentes

  • Autor: danidiaztech
  • Fecha: 2023-06-07 19:29:33 UTC (Hace más de 1 año)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.004 s 1 KBi
#2
Correcto
0.004 s 1 KBi
#3
Correcto
0.004 s 1 KBi
#4
Correcto
0.004 s 1 KBi
#5
Correcto
0.004 s 1 KBi
#6
Correcto
0.005 s 1 KBi
#7
Correcto
0.003 s 1 KBi
#8
Correcto
0.005 s 1 KBi
#9
Correcto
0.005 s 1 KBi
#10
Correcto
0.06 s 2 KBi
#11
Correcto
0.021 s 1 KBi
#12
Correcto
0.047 s 2 KBi
#13
Correcto
0.022 s 1 KBi
#14
Correcto
0.091 s 1 KBi
#15
Correcto
0.051 s 1 KBi
#16
Correcto
0.018 s 1 KBi
#17
Correcto
0.055 s 1 KBi
#18
Correcto
0.041 s 0 KBi
#19
Correcto
0.037 s 1 KBi
#20
Correcto
0.019 s 1 KBi
#21
Correcto
0.073 s 1 KBi
#22
Correcto
0.107 s 1 KBi
#23
Correcto
0.09 s 1 KBi
#24
Correcto
0.098 s 1 KBi
#25
Correcto
0.064 s 1 KBi
#26
Correcto
0.088 s 1 KBi
#27
Correcto
0.055 s 1 KBi
#28
Correcto
0.083 s 1 KBi
#29
Correcto
0.055 s 1 KBi
#30
Correcto
0.026 s 1 KBi
#31
Correcto
0.032 s 2 KBi
#32
Correcto
0.023 s 1 KBi
Puntos totales: 100 / 100

Código

// Made by Daniel Diaz (@Danidiaztech)
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
#define forn(i, n) for (int i = 0; i < n; i++) // for in range in python
#define fore(i, a, b) for (int i = a; i < b; i++) // for in range in python
#define int long long int
#define double long double
#define pb push_back
#define ff first
#define ss second
#define mk make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size() 

typedef pair<int, int> pii;
typedef vector<int> vii;

const int MAX = 1e6;
const int MIN = -MAX;
const int oo = LLONG_MAX / 2;
const int ooo = LLONG_MIN / 2;
const int mod = 1e9 + 7;

// int arr[MAX];
void solve(){
  string s; int k; cin >> s >> k;
  set<char> st;
  map<char,int> mp;
  int ans = oo;
  for (int l = 0, r = 0; r < (int)s.size(); r++){
    mp[s[r]]++;
    st.insert(s[r]);
    while (st.size() >= k){
      ans = min(ans, r - l  + 1);
      // Can try to advance l
      if (mp[s[l]] <= 1){
        // Only remove and advance if size > k
        if (st.size() > k){
          mp[s[l]]--;
          st.erase(s[l]);
          l++;
        }
        else{
          break;
        }
      }
      else{
        // Can advance
        mp[s[l]]--;
        l++;
      }
      ans = min(ans, r - l  + 1);
    }
  }
  cout << (ans == oo ? -1: ans) << endl;
}

int32_t main() {
  fastInp;
  #if LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  #endif

  int tc = 1;
  // cin >> tc;

  for (int t = 1; t <= tc; t++){
    // cout << "Case #" << t << ": ";
    solve();
  }
  return 0;
}