Source Code
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <iomanip>
#include <fstream>
#include <chrono>

using namespace std;

int main()
{

    int n;
    cin >> n;

    string input;
    cin >> input;

    if (n == 1)
    {
        cout << 0 << endl;
        return 0;
    }

    char first = input[0];
    char last = input[n - 1];

    int i;
    for (i = 1; i < n; i++)
    {
        for (int j = 0; (j + i) < n; j++)
        {
            if (input[j] == first && input[j + i] == last)
            {
                cout << n - i - 1 << endl;
                return 0;
            }
        }

        for (int j = n - 1; (j - i) >= n; j--)
        {
            if (input[j] == last && input[j - i] == first)
            {
                cout << n - i - 1 << endl;
                return 0;
            }
        }
    }

    cout << 0 << endl;
    return 0;
}
Copy
Pop Pop DirtyQWERTY
GNU G++17
0 ms
520 KB
Wrong Answer