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

Code 1:
// #include<bits/stdc++.h>

// #define int long long

#include <iostream>

#include <iostream>

#include <chrono>

#include <vector>

#include <math.h>

#include <cmath>

#include <unordered_map>

#include <map>

#include <numeric>

#include <vector>

#include <string>

#include <set>

#include <unordered_set>

#include <queue>

#include <algorithm>

#include <iomanip>

#include <stack>

#include <cstring>

using namespace std;

using namespace chrono;

// #define ll unsigned long long int

#define ll long long int

// ll mod = 1e9 + 7;

ll mod = 998244353;

// ll prime[1000009];

vector<bool> prime(100000000 + 1, true);

ll maxpoweroftwo(ll n)

{

  ll x = log2(n);

  return x;

}

ll maxpoweroffive(ll n)

{

  ll x = log(n) / log(5);

  return x;

}

ll maxpoweroften(ll n)

{

  ll x = log(n) / log(10);

  return x;

}

static ll gcd(ll a, ll b)

{

  ll r, i;

  while (b != 0)

  {

    r = a % b;

    a = b;

    b = r;

  }

  return a;

}

ll lcm(ll a, ll b)

{

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

}

vector<ll> decToBinary(ll n1)

{

  vector<ll> binaryNum(63);

  ll i = 0;

  while (n1 > 0)

  {

    binaryNum[i] = n1 % 2;

    // binaryNum.push_back(n1 % 2);

    n1 = n1 / 2;

    i++;

  }

  return binaryNum;

}

ll binaryToDecimal(vector<ll> v)

{

  ll dec_value = 0;



  // Initializing base value to 1, i.e 2^0

  ll base = 1;



  ll i = v.size() - 1;

  while (i >= 0)

  {

    ll last_digit = v[i];

    // temp = temp / 10;

    dec_value += last_digit * base;



    base = base * 2;

    i--;

  }



  return dec_value;

}

// k = upto k

vector<ll> sieveOfEratosthenes(ll k)

{

  // Create a boolean vector initialized to true

  std::vector<bool> isPrime(k + 1, true);

  isPrime[0] = isPrime[1] = false; // 0 and 1 are not primes



  for (ll i = 2; i * i <= k; ++i)

  {

    if (isPrime[i])

    {

      // Mark multiples of i as not prime

      for (ll j = i * i; j <= k; j += i)

      {

        isPrime[j] = false;

      }

    }

  }



  // Store all prime numbers in a vector

  std::vector<ll> primes;

  for (ll i = 2; i <= k; ++i)

  {

    if (isPrime[i])

    {

      primes.push_back(i);

    }

  }



  return primes;

}

vector<ll> primeFactorization(ll n)

{

  vector<ll> factors;

  for (ll i = 2; i * i <= n; i++)

  {

    ll cnt = 0;

    while (n % i == 0)

    {

      cnt++;

      n /= i;

      factors.push_back(i);

    }

  }

  if (n > 1)

    factors.push_back(n);

  return factors;

}

bool get_bit(ll a, ll i)

{

  return a & (1ll << i);

}

ll binpow(ll a, ll b)

{

  ll ans = 1;

  while (b > 0)

  {

    if ((b & 1) == 1)

      ans *= a;

    a *= a;

    b = b >> 1;

  }

  return ans;

}



ll binpowmod(ll a, ll b)

{

  ll ans = 1;

  while (b > 0)

  {

    if ((b & 1) == 1)

    {

      ans *= a;

      ans %= mod;

    }

    a *= a;

    a %= mod;

    b = b >> 1;

  }

  return ans;

}

const ll MAX = 2e5 + 7;

vector<ll> fact(MAX);



ll add(ll a, ll b)

{

  return (a + b) % mod;

}



ll sub(ll a, ll b)

{

  return ((a - b) % mod + mod) % mod;

}



ll mult(ll a, ll b)

{

  return ((a * b) % mod);

}



ll inv(ll a)

{

  return binpowmod(a, mod - 2);

}



ll divide(ll a, ll b)

{

  return mult(a, inv(b));

}



ll nCr(ll n, ll r)

{

  if (n < r)

    return 0;

  return divide(fact[n], mult(fact[r], fact[n - r]));

}



void preFactorial()

{

  fact[0] = 1;

  for (ll i = 1; i < MAX; i++)

    fact[i] = mult(i, fact[i - 1]);

}

void solve() {

  ll n;cin>>n;string s;cin>>s;

ll counts=0;

ll countp=0;

ll posp=-1;

ll poss=-1;

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

{

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

    poss=i;

    counts++;

  }

  else if(s[i]=='p'){

    posp=i;

    countp++;

  }

}

if(counts==0 || countp==0){

  cout<<"YES";

}

else{

  if(counts>1 && countp>1){

    cout<<"NO";

  }

  else{

    if(counts==1 && countp==1){

      if(poss==0 || posp==n-1){

        cout<<"YES";

      }

      else{

        cout<<"NO";

      }

    }

    else{

    if(counts==1){

      if(poss==0){

        cout<<"YES";

      }

      else{

        cout<<"NO";

      }

    }

    else{

      if(posp==n-1){

        cout<<"YES";

      }

      else{

        cout<<"NO";

      }

    }

  }

  }

}

// sppp => 1234

// sp.p => 1234

// 123456

  cout<<endl;

}



// DIV2 => A -> 15 MIN , B->45MIN , C->1 HR

// BUT LOGIC TIME -> A-> 10 MIN , B-> 30MIN , C-> 30MIN



// DIV3=> A-> 10 MIN , B-> 15MIN , C->45MIN , D->1HR

// BUT LOGIC TIME -> A-> 5 MIN , B-> 10MIN , C-> 30MIN , D-> 30 MIN



// DIV3=> A-> 5 MIN , B-> 10MIN , C->20MIN , D->35MIN , E->45MIN

// BUT LOGIC TIME -> A-> 5 MIN , B-> 5MIN , C-> 15MIN , D-> 25 MIN , E->30MIN



// increase speed in 'a' within 5 min

int main()

{

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);

  preFactorial();

  int T;

  cin >> T;



  auto start1 = high_resolution_clock::now();

  while (T--)

  {

    solve();

  }

  auto stop1 = high_resolution_clock::now();

  auto duration = duration_cast<microseconds>(stop1 - start1);

  cerr << "Time: " << duration.count() / 1000 << " ms" << endl;



  return 0;

}



Code 2:
// #include<bits/stdc++.h>

// #define int long long

#include <iostream>

#include <iostream>

#include <chrono>

#include <vector>

#include <math.h>

#include <cmath>

#include <unordered_map>

#include <map>

#include <numeric>

#include <vector>

#include <string>

#include <set>

#include <unordered_set>

#include <queue>

#include <algorithm>

#include <iomanip>

#include <stack>

#include <cstring>

using namespace std;

using namespace chrono;

// #define ll unsigned long long int

#define ll long long int

// ll mod = 1e9 + 7;

ll mod = 998244353;

// ll prime[1000009];

vector<bool> prime(100000000 + 1, true);

ll maxpoweroftwo(ll n)

{

  ll x = log2(n);

  return x;

}

ll maxpoweroffive(ll n)

{

  ll x = log(n) / log(5);

  return x;

}

ll maxpoweroften(ll n)

{

  ll x = log(n) / log(10);

  return x;

}

static ll gcd(ll a, ll b)

{

  ll r, i;

  while (b != 0)

  {

    r = a % b;

    a = b;

    b = r;

  }

  return a;

}

ll lcm(ll a, ll b)

{

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

}

vector<ll> decToBinary(ll n1)

{

  vector<ll> binaryNum(63);

  ll i = 0;

  while (n1 > 0)

  {

    binaryNum[i] = n1 % 2;

    // binaryNum.push_back(n1 % 2);

    n1 = n1 / 2;

    i++;

  }

  return binaryNum;

}

ll binaryToDecimal(vector<ll> v)

{

  ll dec_value = 0;



  // Initializing base value to 1, i.e 2^0

  ll base = 1;



  ll i = v.size() - 1;

  while (i >= 0)

  {

    ll last_digit = v[i];

    // temp = temp / 10;

    dec_value += last_digit * base;



    base = base * 2;

    i--;

  }



  return dec_value;

}

// k = upto k

vector<ll> sieveOfEratosthenes(ll k)

{

  // Create a boolean vector initialized to true

  std::vector<bool> isPrime(k + 1, true);

  isPrime[0] = isPrime[1] = false; // 0 and 1 are not primes



  for (ll i = 2; i * i <= k; ++i)

  {

    if (isPrime[i])

    {

      // Mark multiples of i as not prime

      for (ll j = i * i; j <= k; j += i)

      {

        isPrime[j] = false;

      }

    }

  }



  // Store all prime numbers in a vector

  std::vector<ll> primes;

  for (ll i = 2; i <= k; ++i)

  {

    if (isPrime[i])

    {

      primes.push_back(i);

    }

  }



  return primes;

}

vector<ll> primeFactorization(ll n)

{

  vector<ll> factors;

  for (ll i = 2; i * i <= n; i++)

  {

    ll cnt = 0;

    while (n % i == 0)

    {

      cnt++;

      n /= i;

      factors.push_back(i);

    }

  }

  if (n > 1)

    factors.push_back(n);

  return factors;

}

bool get_bit(ll a, ll i)

{

  return a & (1ll << i);

}

ll binpow(ll a, ll b)

{

  ll ans = 1;

  while (b > 0)

  {

    if ((b & 1) == 1)

      ans *= a;

    a *= a;

    b = b >> 1;

  }

  return ans;

}



ll binpowmod(ll a, ll b)

{

  ll ans = 1;

  while (b > 0)

  {

    if ((b & 1) == 1)

    {

      ans *= a;

      ans %= mod;

    }

    a *= a;

    a %= mod;

    b = b >> 1;

  }

  return ans;

}

const ll MAX = 2e5 + 7;

vector<ll> fact(MAX);



ll add(ll a, ll b)

{

  return (a + b) % mod;

}



ll sub(ll a, ll b)

{

  return ((a - b) % mod + mod) % mod;

}



ll mult(ll a, ll b)

{

  return ((a * b) % mod);

}



ll inv(ll a)

{

  return binpowmod(a, mod - 2);

}



ll divide(ll a, ll b)

{

  return mult(a, inv(b));

}



ll nCr(ll n, ll r)

{

  if (n < r)

    return 0;

  return divide(fact[n], mult(fact[r], fact[n - r]));

}



void preFactorial()

{

  fact[0] = 1;

  for (ll i = 1; i < MAX; i++)

    fact[i] = mult(i, fact[i - 1]);

}

void solve() {

  ll n;cin>>n;string s;cin>>s;



// pspspsps



// .sp.

// sssssss

// sspp

// 1234

// 4213

// 4321



// sps no

// spps no

// ppps no



// ssss ok

// pppp ok

// sssp ok

// spp  => 123

// .sss => 4321 ok

// .pppp => 12345 ok

// .spp => 1234 no

// .ppps => 12341 no 

// .ssp => 4321 ok



// ss.pp=> 4321





// ristricted case =>

//  p ke bad s

// s ke bad p only one time

ll counts=0;

ll countp=0;

ll posp=-1;

ll poss=-1;

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

{

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

    poss=i;

    counts++;

  }

  else if(s[i]=='p'){

    posp=i;

    countp++;

  }

}

if(counts==0 || countp==0){

  cout<<"YES";

}

else{

  if(countp>1){

    cout<<"NO";

  }

  else{

    if(posp<n-1){

      cout<<"NO";

    }

    else{

      cout<<"YES";

    }



  }

}

// p



  cout<<endl;

}



// DIV2 => A -> 15 MIN , B->45MIN , C->1 HR

// BUT LOGIC TIME -> A-> 10 MIN , B-> 30MIN , C-> 30MIN



// DIV3=> A-> 10 MIN , B-> 15MIN , C->45MIN , D->1HR

// BUT LOGIC TIME -> A-> 5 MIN , B-> 10MIN , C-> 30MIN , D-> 30 MIN



// DIV3=> A-> 5 MIN , B-> 10MIN , C->20MIN , D->35MIN , E->45MIN

// BUT LOGIC TIME -> A-> 5 MIN , B-> 5MIN , C-> 15MIN , D-> 25 MIN , E->30MIN



// increase speed in 'a' within 5 min

int main()

{

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);

  preFactorial();

  int T;

  cin >> T;



  auto start1 = high_resolution_clock::now();

  while (T--)

  {

    solve();

  }

  auto stop1 = high_resolution_clock::now();

  auto duration = duration_cast<microseconds>(stop1 - start1);

  cerr << "Time: " << duration.count() / 1000 << " ms" << endl;



  return 0;

}



Answer: 1