Problem: 2049B - pspspsps
Language: Java 21

Previous submission (verdict: Wrong answer on test 2):
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;

            char arr[] = s.toCharArray();

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

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

                else if(arr[i]=='s' && isS){

                    f = true;

                    break;

                }

            }

            if(s.contains("ps") || s.contains(".sp."))f= true;



            if(f)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();

    }

}



Submission (verdict: Accepted):
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();

    }

}