fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define du double
  5. #define endl '\n'
  6. #define IOS ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
  7.  
  8. int main() {
  9. IOS;
  10. int n;
  11. cin>>n;
  12. string s;
  13. cin>>s;
  14. map<char,int > mp;
  15. for(int i=0;i<n;i++) {
  16. mp[s[i]]++;
  17. }
  18. int l =0;
  19. for(int i=0;i<n;i++) {
  20. mp[s[i]]--;
  21. if(mp[s[i]]==0) {
  22. l=i;
  23. break;
  24. }
  25. }
  26. int r=n-1;
  27. for (int i=n-1;i>=0;i--) {
  28. mp[s[i]]--;
  29. if(mp[s[i]]==0) {
  30. r=i;
  31. break;
  32. }
  33. }
  34.  
  35.  
  36. // cout <<l<< " "<<r<<endl;
  37. cout <<r-l+1;;
  38. return 0;
  39. }
Success #stdin #stdout 0.01s 5312KB
stdin
8
lralllrr
stdout
5