Advertisement
Josif_tepe

Untitled

Dec 26th, 2021
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <map>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. int n;
  10. cin >> n;
  11. string s;
  12. cin >> s;
  13. set<char> st;
  14. for(int i = 0; i < s.size(); i++) {
  15. st.insert(s[i]);
  16. }
  17. map<char, int> cnt;
  18. int result = 2e9;
  19. int unique_chars = st.size();
  20. int curr_uni = 0;
  21. int j = 0;
  22. for(int i = 0; i < n; i++) {
  23.  
  24. while(j < n and curr_uni < unique_chars) {
  25. if(cnt[s[j]] == 0) {
  26. curr_uni++;
  27. }
  28. cnt[s[j]]++;
  29. if(curr_uni == unique_chars) {
  30. result = min(result, j - i + 1);
  31. break;
  32. }
  33. j++;
  34. }
  35. int i_tmp = i;
  36. while(i_tmp < n and curr_uni == unique_chars) {
  37. result = min(result, j - i_tmp + 1);
  38. if(cnt[s[i_tmp]] == 1) {
  39. curr_uni--;
  40. break;
  41. }
  42. cnt[s[i_tmp]]--;
  43. i_tmp++;
  44. }
  45. // i = i_tmp;
  46. }
  47.  
  48. cout << result << endl;
  49. return 0;
  50. }
  51.  
  52. // 7
  53. //aaBCCee
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement