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

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

#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

#define vll vector<long long>

#define ll long long

#define pll pair<ll, ll>

#define mll map<ll, ll>

using namespace std;



int gcd(ll a, ll b) {

    return b == 0 ? a : gcd(b, a % b);

}



long long lcm(long long a, long long b) {

    return (a * b) / gcd(a, b);

}



vector<long long> sieve_of_eratosthenes(long long n) {

    if (n < 2) return {};



    vector<bool> is_prime(n + 1, true);

    is_prime[0] = is_prime[1] = false;



    for (long long p = 2; p * p <= n; ++p) {

        if (is_prime[p]) {

            for (long long i = p * p; i <= n; i += p) {

                is_prime[i] = false;

            }

        }

    }



    vector<long long> primes;

    for (long long p = 2; p <= n; ++p) {

        if (is_prime[p]) {

            primes.push_back(p);

        }

    }

    return primes;

}



ll nCr(ll n, ll r) {

    if (r > n) return 0;

    if (r > n - r) r = n - r; // Because C(n, r) == C(n, n - r)

    ll result = 1;

    for (ll i = 0; i < r; ++i) {

        result *= (n - i);

        result /= (i + 1);

    }

    return result;

}



int findGCD(vector<ll> arr) {

    int result = arr[0];

    for (int i = 1; i < arr.size(); i++) {

        result = gcd(result, arr[i]);

    }

    return result;

}



ll binarySearch(const vector<ll>& arr, ll x) {

    ll left = 0;

    ll right = arr.size() - 1;



    while (left <= right) {

        ll mid = left + (right - left) / 2;



        if (arr[mid] == x)

            return mid;



        if (arr[mid] < x)

            left = mid + 1;

        else

            right = mid - 1;

    }



    return -1; // Element not found

}



bool sortBySecond(const pair<ll, ll>& a, const pair<int, int>& b) {

    return a.second < b.second;

}



void sortBySecondVOP(vector<pair<ll, ll>>& vec) {

    sort(vec.begin(), vec.end(), sortBySecond);

}



void solve() {

    ll n;

    cin >> n;

    string s;

    cin >> s;

    bool fs = false;

    bool fp = false;

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

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

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

        if(fp && s[i] == 's'){

            cout<<"NO"<<endl;

            return;

        }

    }

    fs = false;

    fp = false;

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

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

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

        if(fs && s[i] == 'p'){

            cout<<"NO"<<endl;

            return;

        }

    }

    if(s[0] != 's' && s[n-1]!='p'){

        ll pc = 0;

        ll sc = 0;

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

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

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

        }

        if(sc>0 && pc>0){

            cout<<"NO"<<endl;

            return;

        }

    }

    cout<<"YES"<<endl;

}



int main() {

    fast;

    ll t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}

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

// using namespace std;



// int main(){

//     int t;

//     cin>>t;

//     while (t--){

//         int n;

//         cin>>n;

//         vector<int> v;

//         map<int,int> mp;

//         int x,y;

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

//             cin>>x>>y;

//             mp[x] = y;

//             v.push_back(y);

//         }

//         sort(v.begin(),v.end());

//         long total = 0;

//         for(auto &j : mp){

//             auto p = lower_bound(v.begin(),v.end(),j.second);

//             total+=p-v.begin();

//             v.erase(p);

//         }

//         cout<<total<<endl;

//     }

//     return 0;

// }



#include <bits/stdc++.h>

using namespace std;

 

int main(){

	int t;

	cin>>t;

	while (t--)

	{

		int n;

		string s;

		cin>>n>>s;

		bool is_poss=true,is_p=false,is_s=false;

		if (s[0]=='s') s[0]='.';

		if (s[n-1]=='p') s[n-1]='.';

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

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

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

		}

		if(is_s && is_p == true){

			cout<<"NO"<<endl;

		}else{

			cout<<"YES"<<endl;

		}

	}

}