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: 218 ms):
import java.util.*;
import java.io.*;

public class Main {
	static InputStream inputStream;
	static OutputStream outputStream;
	static InputReader in;
	static PrintWriter out;
	static int MULTIPLE_TESTCASES = 1;
	static String PROB_NAME = "";

	///////////////////////////////////////
	// solution
	///////////////////////////////////////
	static class Solution {

		public void solve(int tc) throws IOException {
			int n = in.ni();
			char[] s = in.ns().toCharArray();
			int sc = 0;
			int pc = 0;
			int dc = 0;

			if (s[0] == '.')
				s[0] = 's';
			if (s[n - 1] == '.')
				s[n - 1] = 'p';
			for (int i = 0; i < n; ++i) {
				if (s[i] == 'p')
					++pc;
				else if (s[i] == 's')
					++sc;
				else
					++dc;
			}
			if (sc == 0 || pc == 0) {
				out.println("YES");
				return;
			}
			if (s[0] == 's' && pc + dc == n - 1) {
				out.println("YES");
				return;
			}
			if (s[n - 1] == 'p' && sc + dc == n - 1) {
				out.println("YES");
				return;
			}
			out.println("NO");
		}
	}

	private static void precompute() {

	}

	///////////////////////////////////////
	// template
	///////////////////////////////////////
	public static void main(String[] args) throws IOException {
		inputStream = System.in;
		outputStream = System.out;
		if (PROB_NAME != "") {
			inputStream = new FileInputStream(new File(PROB_NAME + ".in"));
			outputStream = new FileOutputStream(new File(PROB_NAME + ".out"));
		}
		in = new InputReader(inputStream);
		out = new PrintWriter(outputStream);
		int tc = 1;
		if (MULTIPLE_TESTCASES > 0)
			tc = in.ni();
		precompute();
		Solution sol = new Solution();
		for (int i = 1; i <= tc; ++i) {
			sol.solve(i);
		}
		in.close();
		out.close();
	}

	static class InputReader {
		BufferedReader reader;
		StringTokenizer st;

		public InputReader(InputStream in) {
			reader = new BufferedReader(new InputStreamReader(in));
			st = null;
		}

		public String ns() throws IOException {
			while (st == null || !st.hasMoreTokens()) {
				st = new StringTokenizer(reader.readLine());
			}
			return st.nextToken();
		}

		public Long nl() throws IOException {
			return Long.parseLong(ns());
		}

		public Integer ni() throws IOException {
			return Integer.parseInt(ns());
		}

		public int[] nia(int n) throws IOException {
			int[] a = new int[n];
			for (int i = 0; i < n; ++i) {
				a[i] = ni();
			}
			return a;
		}

		public long[] nla(int n) throws IOException {
			long[] a = new long[n];
			for (int i = 0; i < n; ++i) {
				a[i] = nl();
			}
			return a;
		}

		public void close() throws IOException {
			reader.close();
		}
	}
}