What is wrong with my code? (USACO Bronze Dec 2021 Lonely Photo)

Below is my code. It works for the test case but fails all of the other cases. I’m wondering what is wrong with it. I’ve walked through it mentally, but I still don’t understand why it fails.

import java.util.Scanner;
import java.io.;
import java.util.
;

public class ProblemOne {
    public static void main(String[] args) {
	    Kattio io = new Kattio();
	    int n = io.nextInt();
	    String str = io.next();
	    int totalCombinations = 0;
	    for (int i = 0; i < str.length(); i++) {
		totalCombinations += checkCombinationsFromIndex(i, str);
	    }
	    io.println(totalCombinations);
	    io.close();
    }
    public static int checkCombinationsFromIndex(int startingPoint, String str) {
	    String newSubstring = str.substring(startingPoint);
	   if (newSubstring.length() < 3) {
		   return 0;
	    }
	    int totalSubstrings = 1 + newSubstring.substring(3).length(); 
	    String checkSubstring;
	    int HCounter = 0;
	    int GCounter = 0;
	    int numberOfCombinations = 0;
	    for (int i = 0; i < totalSubstrings; i++) { 
	            checkSubstring = newSubstring.substring(0, 3+i);
		    for (int j = 0; j < checkSubstring.length(); j++) {
			    if (checkSubstring.charAt(j) == 'H') {
				    HCounter++;
			    }
			    if (checkSubstring.charAt(j) == 'G') {
				    GCounter++;
			    }
		    }
		    if (GCounter == 1 || HCounter == 1) {
			    numberOfCombinations++;
		    }
	    }
	    return numberOfCombinations;
    }

static class Kattio extends PrintWriter {
	private BufferedReader reader;
	private StringTokenizer st;

	public Kattio() { 
		this(System.in,System.out); 
	}
	public Kattio(InputStream i, OutputStream o) {
		super(o);
		reader = new BufferedReader(new InputStreamReader(i));
	}
	public Kattio(String problemName) throws IOException {
		super(new FileWriter(problemName+".out"));
		reader = new BufferedReader(new FileReader(problemName+".in"));
	}
	public String next() {
		try {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(reader.readLine());
			return st.nextToken();
		} catch (Exception e) {}
		return null;
	}
	public int nextInt() { return Integer.parseInt(next()); }
	public double nextDouble() { return Double.parseDouble(next()); }
	public long nextLong() { return Long.parseLong(next()); }
}

}

Have you tried this yet?

I’ll take a look at that, thanks!