Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.math.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import java.util.concurrent.*;
- import java.util.function.*;
- import java.util.regex.*;
- import java.util.stream.*;
- import static java.util.stream.Collectors.joining;
- import static java.util.stream.Collectors.toList;
- class Result {
- /*
- * Complete the 'gridSearch' function below.
- *
- * The function is expected to return a STRING.
- * The function accepts following parameters:
- * 1. STRING_ARRAY G
- * 2. STRING_ARRAY P
- */
- public static String gridSearch(List<String> G, List<String> P) {
- // Write your code here
- boolean found = false;
- boolean test = false;
- char[][] grid = grid(G);
- char[][] pGrid = grid(P);
- if (test){
- printGrid(grid);
- printGrid(pGrid);
- }
- for (int i = 0; i <= grid.length-pGrid.length; i++){
- if (found) break;
- char[] match;
- for (int j = 0; j <= grid[0].length-pGrid[0].length; j++){
- match = Arrays.copyOfRange(grid[i], j, j+pGrid[0].length);
- if (Arrays.equals(match, pGrid[0])){
- if (test){
- System.out.println(i + " " + j);
- printLine(match);
- printLine(pGrid[0]);
- }
- boolean allMatch = true;
- int pGridRow = 1;
- for (int k = i+1; k < i+pGrid.length; k++){
- match = Arrays.copyOfRange(grid[k], j, j+pGrid[0].length);
- if (test){
- printLine(match);
- printLine(pGrid[pGridRow]);
- }
- if (!Arrays.equals(pGrid[pGridRow], match)){
- allMatch = false;
- break;
- }
- pGridRow++;
- }
- if (allMatch){
- found = true;
- break;
- }
- }
- }
- }
- return found ? "YES" : "NO";
- }
- public static char[][] grid(List<String> list){
- char[][] grid = new char[list.size()][list.get(0).length()];
- for (int i = 0; i < grid.length; i++){
- grid[i] = list.get(i).toCharArray();
- }
- return grid;
- }
- public static void printGrid(char[][] grid){
- for (int i = 0; i < grid.length; i++){
- for (int j = 0; j < grid[0].length; j++){
- System.out.print(grid[i][j]);
- }
- System.out.println("");
- }
- }
- public static void printLine(char[] line){
- for (char c : line){
- System.out.print(c);
- }
- System.out.println("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement