Need Help Understanding USACO Bronze 2022 Photoshoot Open Contest Problem #1 Solution

I understand what this problem asks for, but I just can’t think of a way to solve this. Am I missing DSA knowledge?? I’ve looked at the solution for this problem and it just doesn’t make sense to me. Here the solution to it:
http://www.usaco.org/index.php?page=viewproblem2&cpid=1227

import java.io.*;

public class Photoshoot {
public static void main(String[] args) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int cowNum = Integer.parseInt(read.readLine());
String cows = read.readLine();
assert cows.length() == cowNum && cowNum % 2 == 0;
read.close();

	int flips = 0;
	for (int c = cowNum - 2; c >= 0; c -= 2) {
		String sub = cows.substring(c, c + 2);
		if (sub.charAt(0) == sub.charAt(1)) { continue; }
		if ((sub.equals("GH") && flips % 2 == 0) ||
		    (sub.equals("HG") && flips % 2 == 1)) {
			flips++;
		}
	}

	System.out.println(flips);
}

}

I don’t get the logic behind going through every pair of cows and checking for which substring they are and the conditions where it’s trying to see if the variable ‘flips’ is even or odd. None of it makes sense. Is this just a difficult problem or am I seriously just missing DSA knowledge??