I’ve written an answer for The Grid Search challenge on HackerRank. Please let me know of any improvements that can be made to my implementation (specifically if there's any way to make the algorithm more efficient. I'd like to make this algorithm optimal).
P.S: My implementation is a brute force algorithm, and I reckon that there's a much better way of solving this problem. But, as the grid is unsorted, I'm not exactly sure what that better method would be.
Problem statement (as written on HackerRank):
Given a 2D array of digits, try to find the location of a given 2D pattern of digits.
Answer:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
Solution solution = new Solution();
try(BufferedReader input = new BufferedReader(new InputStreamReader(System.in))){
int numTestCases = Integer.parseInt(input.readLine());
assert numTestCases >= 1 && numTestCases <= 5;
int[][] grid;
int[][] pattern;
for(int t = 0; t < numTestCases; t++) {
grid = solution.buildArray(input);
pattern = solution.buildArray(input);
// Gotta STDOUT in HackerRank for problem to be graded.
System.out.println(solution.findPattern(grid, pattern));
}
}catch(IOException e){
e.printStackTrace();
}
}
/**
* Determines whether pattern exists in grid.
*
* @param grid 2D array of digits
* @param pattern the 2D sequence of digits to be found in grid
* @return "YES" if grid contains pattern. "NO" if not.
*/
public String findPattern(int[][] grid, int[][] pattern){
// R, C, r, & c are the same letters used in the problem
for(int R = 0; R < grid.length - pattern.length + 1; R++){
for(int C = 0; C < grid[0].length - pattern[0].length + 1; C++){
outerLoop:
for(int r = 0; r < pattern.length; r++){
for(int c = 0; c < pattern[0].length; c++){
if(grid[R + r][C + c] != pattern[r][c]){
break outerLoop;
}
}
if(r == pattern.length - 1){
return "YES";
}
}
}
}
return "NO";
}
/**
* Builds and returns an array built according to data received from STDIN.
*
* @param input STDIN stream
* @return a 2D primitive array containing 0-9 digits
* @throws IOException
*/
public int[][] buildArray(BufferedReader input) throws IOException {
String[] sizeParameters = input.readLine().split(" ");
int rows = Integer.parseInt(sizeParameters[0]);
int columns = Integer.parseInt(sizeParameters[1]);
int[][] array = new int[rows][columns];
for(int i = 0; i < rows; i++){
String rowOfNumbers = input.readLine();
for(int j = 0; j < columns; j++){
array[i][j] = Character.getNumericValue(rowOfNumbers.charAt(j));
}
}
return array;
}
}
BufferedReader
. Have a look on the Input Format part of the problem statement for details: hackerrank.com/challenges/the-grid-search \$\endgroup\$