Source Code
#include <iostream>
#include <algorithm>
#include <iterator>
#include <math.h>
#include <vector>
#include <set>
using namespace std;
int main() {
	int n;
	string s;
	cin >> n >> s;
	char x = s[0];
	char y = s[n - 1];
	int  c1 = 0, c2 = 0;
	int c3 = 0, c4 = 0;
	for (int i = 0; i < n - 1; i++) {
		if (s[i] == x) {
			c1 = i;
		}
	}
	for (int j = n - 1; j > c1; j--) {
		if (s[j] == y) {
			c2 = j;
		}
	}
	for (int j = n - 1; j >= 1; j--) {
		if (s[j] == y) {
			c3 = j;
		}
	}
	for (int i = 0; i < c3; i++) {
		if (s[i] == x) {
			c4 = i;
		}
	}
	cout << max(n - (c2 - c1 + 1), n - (c3 - c4 + 1));
	return 0;
}
Copy
Pop Pop Sarah
GNU G++17
0 ms
416 KB
Wrong Answer