Source Code
#include<iostream>
using namespace std;
#include <chrono>
using namespace std::chrono;

#include<stack>
#include<queue>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include <functional>
#include<map>
#include<iomanip>
#include<cmath>
#include<list>

#define fst ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define pii pair<int,int>
#define vll vector<ll>
#define fin(i,v) for(ll i=0;i<v.size();i++)
#define go(x,y) for(int i=x;i<=y;i++)
#define MOD 1e9+7
#define out(x) cout << x << endl
#define mii map<int,int>
#define mid map<int,double
#define mm(arr) memset(arr, 0, sizeof(arr))

#define two2n(n) (1<<n)
constexpr auto PI = 3.14159265358979323846;;



bool ispow(unsigned long long x)
{
	return (x != 0) && ((x & (x - 1)) == 0);
}


bool how(pair<int, int>& a, pair<int, int>& b)
{
	return ((a.second > b.second));//|| (a.second == b.second && a.first < b.first));
}


/*
        auto start = high_resolution_clock::now();
        auto stop = high_resolution_clock::now();
		auto duration = duration_cast<microseconds>(stop - start);
		cout << "time needed is : " << fixed << setprecision(8) << duration.count() << "   microseconds" << endl;

*/





bool isprime(string s,int mx,int x)
{
	for (int i = 2; i <= x; i++)
	{
		if (x % i == 0)
		{
			if (i >= mx || x / i >= mx)
			{
				int cur = 0;
				bool ok = true;
				for (int j = 0; j < s.length(); j++)
				{
					cur += (s[j] - 48);
					if (cur == x / i)
						cur = 0;
				}
				if(cur==0)
			return false;
			}
		}
	}

	return true;
}


bool prime(int n)
{
	if (n <= 1)
		return false;

	
	for (int i = 2; i < n; i++)
		if (n % i == 0)
			return false;

	return true;
}



int main()
{
	/*
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
   */
     fst;

	 

	 int n;
	 cin >> n;
	 string s;
	 cin >> s;

	 char f = s[0], l = s[n - 1];

	 int dist = n;
	 bool same = false;
	 if (f == l)
		 same = true;
	 
	 if (same)
	 {
		 dist = n - 1;
	 }
	 else
	 {
		 for (int i = 0; i < n ; )
		 {
			 int j = i + 1;
			 while (j < n && s[j] == f)
			 {
				 j++;
			 }
			 if(j<n)
			 dist = min(dist, (j - i)+1);
			 i = j;
		 }
	 }

	 cout << n - dist;


	 
	 


	return 0;
}


	
Copy
Pop Pop M_kmail
GNU G++17
1 ms
192 KB
Wrong Answer