getting NZEC java !!

Here is my code :

import java.io.BufferedReader;    
import java.io.InputStreamReader;    
import java.io.IOException;    

public class Main 
{     
    static final char[] spoon = {'p','o','o','n'};
    static final char[] SPOON = {'P','O','O','N'};
    
    static BufferedReader br;
    static String[] result;
    
    public static void main(String[] args)throws IOException{
        br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        result = new String[n];
        for(int ctr = 1;ctr <= n;ctr++)
            process(ctr-1);
        for (String string : result) {
            System.out.println(string);
        }
    }
    
    public static void process(int ctr) throws IOException
    {
        String line = br.readLine();
        String sr,sc;
        sr = line.substring(0,line.indexOf(" ")).trim();
        sc = line.substring(line.lastIndexOf(" ")).trim();
        int r = Integer.parseInt(sr);
        int c = Integer.parseInt(sc);
        char matrix[][] = new char[r][c];
        for(char[] array : matrix)
        {
            String str = br.readLine();
            for(int i = 0;i < c;i++)
                array[i] = str.charAt(i);
        }
        for (int idx = 0; idx < r; idx++) 
        {
            for (int col = 0; col < c; col++) 
            {
                if (matrix[idx][col] == 's' || matrix[idx][col] == 'S') 
                {
                    int row = idx;
                    int column = col;
                    if (spoonInRow(matrix, idx, col)) 
                    {
                        result[ctr] = "There is a spoon!";
                        return;
                    }
                    else if (spoonInColumn(matrix,idx,col)) 
                    {
                        result[ctr] = "There is a spoon!";
                        return;
                    }
                }
            }
        }
        result[ctr] = "There is indeed no spoon!";
    }
    
    private static boolean spoonInRow(char[][] array,int ri,int ci)
    {
        int r = array.length;
        int counter = 0;
        ri++;
        while (ri < r) 
        {
            if((array[ri][ci] == spoon[counter]) || (array[ri][ci] == SPOON[counter]))
            {
                ri++;
                counter++;
                continue;
            }
            return false;
        }
        return counter == 4;
    }
    
    private static boolean spoonInColumn(char[][] array,int ri,int ci)
    {
        int c = array[0].length;
        int counter = 0;
        ci++;
        while (ci < c) 
        {            
            char ch = array[ri][ci];
            if((ch == spoon[counter]) || (ch == SPOON[counter]))
            {
                ci++;
                counter++;
                continue;
            }
            return false;
        }
        return counter == 4;
    }  
}

it runs perfectly for the test cases in my PC !
this problem

I have read the faq could not find my solution there

The approach to the solution is write but there are some cases where the arrays goes out of bounds and hence you will get NZEC. In the SpoonInRow you are just simply incrementing column number but not checking whether the size of “counter” variable is exceeding the size of spoon array. Similarly for the other method named SpoonInColumn.

1 Like