USACO Bronze 2019 December - Cow Gymnastics

I don’t see anything wrong with my code, but I can only get through the first test case. Any suggestions?

import java.util.*;
import java.io.*;
public class Test {
    public static void main(String[] args) throws IOException{
        File text = new File("gymnastics.in");
        Scanner input = new Scanner(text);
        File outFile = new File ("gymnastics.out");
        FileWriter fWriter = new FileWriter (outFile);
        PrintWriter pWriter = new PrintWriter (fWriter);

        int K = input.nextInt(); int N = input.nextInt();
        String[] sessions = new String[K];
        input.nextLine();
        for (int i=0; i<K; i++) {
            sessions[i] = input.nextLine();
        }

        int count = 0;
        for (int a=1; a<=N; a++) {
            for (int b=1; b<=N; b++) {
                int c = 0;
                String a_ = a + "";
                String b_ = b + "";
                for (int i=0; i<K; i++) {
                    if (sessions[i].indexOf(a_) < sessions[i].indexOf(b_)) {
                        c++;
                    }
                }
                if (c==K) {
                    //System.out.println(a + " " + b);
                    count++;
                }

            }
        }

        pWriter.println(count);
        pWriter.close();
    }
}
  1. The problem specifies comparisons among distinct cows but the nested for loop will compare cow i with itself
  2. Note that sessions[i].indexOf() searches for the first occurrence of the the specified characters. However say you have “10 9 8…1” if you search for index of 1, it will return the position of 10, i.e. 0.

The sample test case ACs because N<10

Try to use String.toCharArray() and search for the indices of a and b instead