fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b) {
  14. int x = 1;
  15. a %= M;
  16. while (b) {
  17. if (b & 1) x = (x * a) % M;
  18. a = (a * a) % M;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. string a;
  31.  
  32. int consistency1(int n){
  33. int ans = n;
  34.  
  35. unordered_set<int> st;
  36. for(int i=0; i<n; i++){
  37. st.insert(a[i]);
  38. }
  39. int k = st.size();
  40.  
  41. unordered_map<int,int> mp;
  42.  
  43. int s = 0, e = 0;
  44. while(e<n){
  45. mp[a[e]]++;
  46. if(mp.size() < k){
  47. e++;
  48. }
  49. else{
  50.  
  51. while(s<=e && mp.size() >= k){
  52. ans = min(ans, e-s+1);
  53. mp[a[s]]--;
  54. if(mp[a[s]] == 0) mp.erase(a[s]);
  55. s++;
  56. }
  57. e++;
  58. }
  59. }
  60.  
  61.  
  62. return ans;
  63. }
  64.  
  65.  
  66. int consistency2(int n){
  67. int ans = n;
  68.  
  69. unordered_set<int> st;
  70. for(int i=0; i<n; i++){
  71. st.insert(a[i]);
  72. }
  73. int k = st.size();
  74.  
  75. unordered_map<int,int> mp;
  76.  
  77. int s = 0, e = 0;
  78. while(e<n){
  79. mp[a[e]]++;
  80. while(s<=e && mp.size() >= k){
  81. ans = min(ans, e-s+1);
  82. mp[a[s]]--;
  83. if(mp[a[s]] == 0) mp.erase(a[s]);
  84. s++;
  85. }
  86. e++;
  87. }
  88.  
  89.  
  90. return ans;
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. int practice(int n){
  105.  
  106.  
  107. return 0;
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. void solve() {
  115.  
  116.  
  117. int n;
  118. cin>> n;
  119.  
  120. cin >> a;
  121.  
  122.  
  123. cout << consistency2(n) << endl;
  124.  
  125.  
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. int32_t main() {
  133. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  134.  
  135. int t = 1;
  136. // cin >> t;
  137. while (t--) {
  138. solve();
  139. }
  140.  
  141. return 0;
  142. }
Success #stdin #stdout 0s 5316KB
stdin
6
aaBCCe
stdout
5