Problem: 2049B - pspspsps
Language: java

Solution 1 (runtime: 312 ms):
import java.util.*;



public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int test = sc.nextInt();

		for (int z = 0; z < test; z++) {



			int n = sc.nextInt();

			String s= sc.next();

			int counts=0;

			int countp=0;

			int lasts =0;

			int firstp=Integer.MAX_VALUE;

			for(int i =0;i<s.length();i++){

			    if(s.charAt(i)=='s'){

			        counts++;

			        lasts=i;

			    }else if(s.charAt(i)=='p'){

			        countp++;

			        firstp=Math.min(firstp,i);

			    }

			}

			if(firstp<lasts){

			    System.out.println("NO");

			}

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

			    System.out.println("NO");

			}

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

			    if(s.charAt(n-1)=='p'){

			        System.out.println("YES");

			    }else{

			        System.out.println("NO");

			    }

			    

			}else if(countp==0 && counts>1){

			    System.out.println("YES");

			}

			

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

			    if(s.charAt(0)=='s'){

			        System.out.println("YES");

			    }else{

			        System.out.println("NO");

			    }

			}

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

			    if(s.charAt(0)=='s' || s.charAt(s.length()-1)=='p'){

			        System.out.println("YES");

			    }else{

			        System.out.println("NO");

			    }

			}

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

			    System.out.println("YES");

			}

			else{

			    System.out.println("YES");

			}

			

		}

	}

}

Solution 2 (runtime: 312 ms):
import java.util.*;

import java.io.*;



public class B_pspspsps {



    static final int MAX = 10_000_000;

    static final int MOD = 1_000_000_007;

    static boolean[] isPrime = new boolean[MAX + 1];

    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));



    public static void solve() {

        FastIO sc = new FastIO();

        int testCases = sc.nextInt();



        while (testCases-- != 0) {

            int n = sc.nextInt();

            String s = sc.next();

            boolean f = false;

            boolean isS=false;

            boolean isP=false;

            char arr[] = s.toCharArray();

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

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

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

                if(arr[i]=='p')isP=true;

                if(arr[i]=='s')isS=true;

            }



            if(isP&&isS)out.println("NO");

            else

            out.println("YES");

        }

        out.flush();

    }



    public static void debugArray(int[] arr) {

        out.println(Arrays.toString(arr));

    }



    public static void debugList(List<Integer> list) {

        out.println(list);

    }



    public static long modAdd(long a, long b, int mod) {

        return (a + b) % mod;

    }



    public static long modMul(long a, long b, int mod) {

        return (a * b) % mod;

    }



    public static long modExp(long base, long exp, int mod) {

        long result = 1;

        while (exp > 0) {

            if ((exp & 1) == 1) result = (result * base) % mod;

            base = (base * base) % mod;

            exp >>= 1;

        }

        return result;

    }



    public static void precomputePrimes() {

        Arrays.fill(isPrime, true);

        isPrime[0] = isPrime[1] = false;

        for (int i = 2; i * i <= MAX; i++) {

            if (isPrime[i]) {

                for (int j = i * i; j <= MAX; j += i) {

                    isPrime[j] = false;

                }

            }

        }

    }



    public static boolean isPrime(int n) {

        return n >= 0 && n <= MAX && isPrime[n];

    }



    public static void bfs(int start, List<Integer>[] graph, boolean[] visited) {

        Queue<Integer> queue = new LinkedList<>();

        queue.add(start);

        visited[start] = true;



        while (!queue.isEmpty()) {

            int node = queue.poll();

            for (int neighbor : graph[node]) {

                if (!visited[neighbor]) {

                    queue.add(neighbor);

                    visited[neighbor] = true;

                }

            }

        }

    }



    static class DSU {

        int[] parent, rank;



        DSU(int n) {

            parent = new int[n];

            rank = new int[n];

            for (int i = 0; i < n; i++) parent[i] = i;

        }



        int find(int x) {

            if (x != parent[x]) parent[x] = find(parent[x]);

            return parent[x];

        }



        void union(int x, int y) {

            int rootX = find(x), rootY = find(y);

            if (rootX != rootY) {

                if (rank[rootX] > rank[rootY]) parent[rootY] = rootX;

                else if (rank[rootX] < rank[rootY]) parent[rootX] = rootY;

                else {

                    parent[rootY] = rootX;

                    rank[rootX]++;

                }

            }

        }

    }



    static class FastIO {

        BufferedReader br;

        StringTokenizer st;



        public FastIO() {

            br = new BufferedReader(new InputStreamReader(System.in));

        }



        String next() {

            while (st == null || !st.hasMoreElements()) {

                try {

                    st = new StringTokenizer(br.readLine());

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            return st.nextToken();

        }



        int nextInt() {

            return Integer.parseInt(next());

        }



        long nextLong() {

            return Long.parseLong(next());

        }



        double nextDouble() {

            return Double.parseDouble(next());

        }



        String nextLine() {

            String str = "";

            try {

                str = br.readLine();

            } catch (IOException e) {

                e.printStackTrace();

            }

            return str;

        }

    }



    public static void main(String[] args) {

        precomputePrimes();

        solve();

    }

}