Problem: 2049B - pspspsps
Language: c++23 

Code 1:
            /***>  @author : a_e_kasem  <***/



//                        ﷽

//                  { وَأَنْ لَيْسَ لِلْإِنْسَانِ إِلَّا مَا سَعَى }



//                           {***}



//         فَالجُهدُ يُثمِرُ إنْ تَضافَرَ صَفوُهُ، والعَزمُ يَرفعُ صَرحَ كُلِّ بُنيانِ

//

//                    وَما نَيلُ المَطالِبِ بِالتَمَنّي

//                     وَلَكِن تُؤخَذُ الدُنيا غِلابا

//                           ***

//                   وَما اِستَعصى عَلى قَومٍ مَنالٌ

//                      إِذا الإِقدامُ كانَ لَهُم رِكا

//

#include <bits/stdc++.h>

using namespace std;



#define int                 long long

#define ld                  long double

#define ull                 unsigned long long

#define w(x)                int x; cin >> x; while(x--)

#define vi                  vector<int>

#define vvi                 vector<vector<int> >

#define fi                  first

#define se                  second

#define pii                 pair<int, int>

#define vpii                vector<pair<int, int> >

#define FOR(i, a, b)        for (int(i) = (a); (i) <= (b); ++(i))

#define RFOR(i, a, b)       for (int(i) = (a)-1; (i) >= (b); --(i))

#define FORALL(i, a)        for (auto& (i) : (a))

#define printall(a)         for (auto& (i) : (a)) cout << i << ' '

#define cinall(a)           for (auto& (i) : (a)) cin >> i

#define cin_1all(a, n)      for (int (i) = 1; (i) <= (n) ; i++) cin >> a[i]

#define rsort(a)            sort((a).rbegin(), (a).rend())

#define all(c)              (c).begin(), (c).end()

#define pqmn                priority_queue<int, vector<int>, greater<int> >

#define mx_all(c)           *max_element((c).begin(), (c).end())

#define mn_all(c)           *min_element((c).begin(), (c).end())

#define FIO                 ios::sync_with_stdio(false); cin.tie(nullptr);

#define in                  freopen("input.txt", "r", stdin);

#define el                  cout << "\n"

#define no                  cout << "No" << endl

#define yes                 cout << "Yes" << endl

#define NO                  cout << "NO" << endl

#define YES                 cout << "YES" << endl



const int INF = (long long)2e9;

const int MOD = (long long) 7+1e9;

const ld  PI  = 3.1415926535897932384626;



void FASTIO();

void solve();

int32_t main() {

    FASTIO();



    w(t)

    {

        solve();

    }





    // solve();

}



/*===================================================================//



       ██╗  ██╗     █████╗     ███████╗    ███████╗    ███╗   ███╗

       ██║ ██╔╝    ██╔══██╗    ██╔════╝    ██╔════╝    ████╗ ████║

       █████╔╝     ███████║    ███████╗    █████╗      ██╔████╔██║

       ██╔═██╗     ██╔══██║    ╚════██║    ██╔══╝      ██║╚██╔╝██║

       ██║  ██╗    ██║  ██║    ███████║    ███████╗    ██║ ╚═╝ ██║

       ╚═╝  ╚═╝    ╚═╝  ╚═╝    ╚══════╝    ╚══════╝    ╚═╝     ╚═╝



//====================================================================*/



void FASTIO()

{

    FIO



#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    freopen("error.txt", "w", stderr);

#endif

}

int ctoi(char c) {

    return (c - '0');

}

int sum_of_digits(int n) {

    string s = to_string(n);

    return s.size();

}



void solve() {

    int n; cin >> n;

    vector<char> a(n);

    int p = 0, s = 0;



    for (int i = 0; i < n; i++)

    {

        cin >> a[i];

        if (a[i] == 'p') p++;

        if (a[i] == 's') s++;

    }



    if (!s || !p) YES;

    else if (p == 1 && a.back() == 'p') YES;

    else if (s == 1 && a.front() == 's') YES;

    else NO;

}



Code 2:
            /***>  @author : a_e_kasem  <***/



//                        ﷽

//                  { وَأَنْ لَيْسَ لِلْإِنْسَانِ إِلَّا مَا سَعَى }



//                           {***}



//         فَالجُهدُ يُثمِرُ إنْ تَضافَرَ صَفوُهُ، والعَزمُ يَرفعُ صَرحَ كُلِّ بُنيانِ

//

//                    وَما نَيلُ المَطالِبِ بِالتَمَنّي

//                     وَلَكِن تُؤخَذُ الدُنيا غِلابا

//                           ***

//                   وَما اِستَعصى عَلى قَومٍ مَنالٌ

//                      إِذا الإِقدامُ كانَ لَهُم رِكا

//

#include <bits/stdc++.h>

using namespace std;



#define int                 long long

#define ld                  long double

#define ull                 unsigned long long

#define w(x)                int x; cin >> x; while(x--)

#define vi                  vector<int>

#define vvi                 vector<vector<int> >

#define fi                  first

#define se                  second

#define pii                 pair<int, int>

#define vpii                vector<pair<int, int> >

#define FOR(i, a, b)        for (int(i) = (a); (i) <= (b); ++(i))

#define RFOR(i, a, b)       for (int(i) = (a)-1; (i) >= (b); --(i))

#define FORALL(i, a)        for (auto& (i) : (a))

#define printall(a)         for (auto& (i) : (a)) cout << i << ' '

#define cinall(a)           for (auto& (i) : (a)) cin >> i

#define cin_1all(a, n)      for (int (i) = 1; (i) <= (n) ; i++) cin >> a[i]

#define rsort(a)            sort((a).rbegin(), (a).rend())

#define all(c)              (c).begin(), (c).end()

#define pqmn                priority_queue<int, vector<int>, greater<int> >

#define mx_all(c)           *max_element((c).begin(), (c).end())

#define mn_all(c)           *min_element((c).begin(), (c).end())

#define FIO                 ios::sync_with_stdio(false); cin.tie(nullptr);

#define in                  freopen("input.txt", "r", stdin);

#define el                  cout << "\n"

#define no                  cout << "No" << endl

#define yes                 cout << "Yes" << endl

#define NO                  cout << "NO" << endl

#define YES                 cout << "YES" << endl



const int INF = (long long)2e9;

const int MOD = (long long) 7+1e9;

const ld  PI  = 3.1415926535897932384626;



void FASTIO();

void solve();

int32_t main() {

    FASTIO();



    w(t)

    {

        solve();

    }





    // solve();

}



/*===================================================================//



       ██╗  ██╗     █████╗     ███████╗    ███████╗    ███╗   ███╗

       ██║ ██╔╝    ██╔══██╗    ██╔════╝    ██╔════╝    ████╗ ████║

       █████╔╝     ███████║    ███████╗    █████╗      ██╔████╔██║

       ██╔═██╗     ██╔══██║    ╚════██║    ██╔══╝      ██║╚██╔╝██║

       ██║  ██╗    ██║  ██║    ███████║    ███████╗    ██║ ╚═╝ ██║

       ╚═╝  ╚═╝    ╚═╝  ╚═╝    ╚══════╝    ╚══════╝    ╚═╝     ╚═╝



//====================================================================*/



void FASTIO()

{

    FIO



#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    freopen("error.txt", "w", stderr);

#endif

}

int ctoi(char c) {

    return (c - '0');

}

int sum_of_digits(int n) {

    string s = to_string(n);

    return s.size();

}



void solve() {

    int n; cin >> n;

    vector<char> a(n);

    int p = 0, s = 0;



    for (int i = 0; i < n; i++)

    {

        cin >> a[i];

        if (a[i] == 'p') p++;

        if (a[i] == 's') s++;

    }



    if (!s || !p) YES;

    else if (s == 1 && a.back() == 'p') YES;

    else if (p == 1 && a.front() == 's') YES;

    else NO;

}



Answer: 1