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

Solution 1 (runtime: 46 ms):
#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>



template <typename T>

using ordered_set =

    __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,

                     __gnu_pbds::tree_order_statistics_node_update>;

using pi = pair<int, int>;

using pl = pair<long long, long long>;

using vi = vector<int>;

using vl = vector<long long>;

using vvi = vector<vector<int>>;

using vvl = vector<vector<long long>>;

using vpi = vector<pair<int, int>>;

using vpl = vector<pair<long long, long long>>;

using vvpi = vector<vector<pair<int, int>>>;

using vvpl = vector<vector<pair<long long, long long>>>;

using vb = vector<bool>;

using vs = vector<string>;

using si = set<int>;

using mpii = map<int, int>;



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

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

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

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



#define pb push_back

#define sc static_cast

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

#define rall(v) v.rbegin(), v.rend()

#define sz(x) (int32_t)x.size()

#define yes cout << "YES\n";

#define no cout << "NO\n";

#define approx(a) fixed << setprecision(a)

#define ff first

#define ss second

#define INF LONG_LONG_MAX

#define INFF LONG_LONG_MIN

#define inf INT_MAX

#define inff INT_MIN

#define nl '\n'



template <class T> void read(vector<T> &v);

template <class F, class S> void read(pair<F, S> &p);

template <class T, size_t Z> void read(array<T, Z> &a);

template <class T> void read(T &x) {

  cin >> x;

}

template <class R, class... T> void read(R &r, T &...t) {

  read(r);

  read(t...);

};

template <class T> void read(vector<T> &v) {

  for (auto &x : v)

    read(x);

}

template <class F, class S> void read(pair<F, S> &p) {

  read(p.ff, p.ss);

}

template <class T, size_t Z> void read(array<T, Z> &a) {

  for (auto &x : a)

    read(x);

}



template <class F, class S> void pr(const pair<F, S> &x);

template <class T> void pr(const T &x) {

  cout << x;

}

template <class R, class... T> void pr(const R &r, const T &...t) {

  pr(r);

  pr(t...);

}

template <class F, class S> void pr(const pair<F, S> &x) {

  pr("{", x.ff, ", ", x.ss, "}");

}

void ps() {

  pr("\n");

}

template <class T> void ps(const T &x) {

  pr(x);

  ps();

}

template <class T> void ps(vector<T> &v) {

  for (auto &x : v)

    pr(x, ' ');

  ps();

}

template <class T, size_t Z> void ps(const array<T, Z> &a) {

  for (auto &x : a)

    pr(x, ' ');

  ps();

}

template <class F, class S> void ps(const pair<F, S> &x) {

  pr(x.ff, ' ', x.ss);

  ps();

}

template <class R, class... T> void ps(const R &r, const T &...t) {

  pr(r, ' ');

  ps(t...);

}

#define definp()                                                               \

  int n;                                                                       \

  read(n);                                                                     \

  vl a(n);                                                                     \

  read(a);

#define int long long



// int dx[4] = {-1, 1, -1, 1}, dy[4] = {-1, -1, 1, 1};

#define MOD (long long)(1e9 + 7)

void exec_once() {

}

bool tc = 1;



void solve(int tn [[maybe_unused]]) {

    int n;read(n);

    string s;

    read(s);

    int cur = -1;

    bool p = 0;

    int cnt1 = 0, cnt2=0;

    fori(i,0,n){

        if(s[i] == 's'&&p)return ps("NO");

        if(s[i] == 'p')p=1;

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

        else if(s[i]=='p')cnt2++;

    }

    if(cnt1==1 && (s[0] == 's' || s[n-1]=='s')) return ps("YES");

    if(cnt2==1 && (s[0] == 'p' || s[n-1]=='p')) return ps("YES");

    if(cnt1==0 || cnt2 == 0) return ps("YES");

    ps("NO");

}



int32_t main() {

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);

  exec_once();

  int t = 1;

  if (tc)

    cin >> t;

  fore(tn, 1, t) solve(tn);

  return 0;

}



Solution 2 (runtime: 30 ms):
#include<bits/stdc++.h>

using namespace std;



void solve() {

    int n;

    string s;

    cin >> n;

    cin >> s;

    bool seen_s=false, seen_p=false;

    for (int i = 1; i < n-1; i++) {

        if (s[i] == 's') seen_s = true;

        if (s[i] == 'p') seen_p = true;

    }

    seen_s |= s.back() == 's';

    seen_p |= s[0] == 'p';

    if (seen_s && seen_p) cout << "NO\n";

    else cout << "YES\n";

}



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    int t;

    cin >> t;

    while (t--) solve();

}