Problem: 2049B - pspspsps
Language: java

Solution 1 (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();

    }

}



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

public class psp {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        for(int i = 0; i < tc; i++){
            int n = sc.nextInt();
            sc.nextLine();
            String s = sc.nextLine();
            String[] ps = new String[n];
            for(int j = 0; j < n; j++){
                ps[j] = s.substring(j, j+1);
            }
            //set up

            //test1
            String r = "yes";
            boolean pe = false;
            for(int j = 0; j < n; j++){
                if(ps[j].equals("p")){
                    pe = true;
                }
                if(pe && ps[j].equals("s")){
                    r = "no";
                    break;
                }
            }
            boolean se = false;
            for(int j = 1; j < n-1; j++){
                if(ps[j].equals("s")){
                    se = true;
                }
                if(se && ps[j].equals("p")){
                    r = "no";
                    break;
                }
            }
            System.out.println(r);
        }
    }
    
}