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: 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();
		}
	}
}