Advertisement
YChalk

The Grid

Feb 23rd, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12.  
  13. class Result {
  14.  
  15.     /*
  16.      * Complete the 'gridSearch' function below.
  17.      *
  18.      * The function is expected to return a STRING.
  19.      * The function accepts following parameters:
  20.      *  1. STRING_ARRAY G
  21.      *  2. STRING_ARRAY P
  22.      */
  23.  
  24.     public static String gridSearch(List<String> G, List<String> P) {
  25.     // Write your code here
  26.     boolean found = false;
  27.     boolean test = false;
  28.     char[][] grid = grid(G);
  29.     char[][] pGrid = grid(P);
  30.    
  31.     if (test){
  32.         printGrid(grid);
  33.         printGrid(pGrid);
  34.     }
  35.    
  36.    
  37.     for (int i = 0; i <= grid.length-pGrid.length; i++){
  38.         if (found) break;
  39.         char[] match;
  40.         for (int j = 0; j <= grid[0].length-pGrid[0].length; j++){
  41.             match = Arrays.copyOfRange(grid[i], j, j+pGrid[0].length);
  42.            
  43.             if (Arrays.equals(match, pGrid[0])){
  44.                 if (test){
  45.                 System.out.println(i + " " + j);
  46.                 printLine(match);
  47.                 printLine(pGrid[0]);
  48.                 }
  49.                 boolean allMatch = true;
  50.                 int pGridRow = 1;
  51.                 for (int k = i+1; k < i+pGrid.length; k++){
  52.                     match = Arrays.copyOfRange(grid[k], j, j+pGrid[0].length);
  53.                     if (test){
  54.                         printLine(match);
  55.                         printLine(pGrid[pGridRow]);
  56.                     }
  57.                     if (!Arrays.equals(pGrid[pGridRow], match)){
  58.                         allMatch = false;
  59.                         break;
  60.                     }
  61.                     pGridRow++;
  62.                 }
  63.                 if (allMatch){
  64.                     found = true;
  65.                     break;
  66.                 }
  67.                
  68.             }
  69.         }
  70.     }
  71.    
  72.     return found ? "YES" : "NO";
  73.     }
  74.    
  75.     public static char[][] grid(List<String> list){
  76.         char[][] grid = new char[list.size()][list.get(0).length()];
  77.        
  78.         for (int i = 0; i < grid.length; i++){
  79.             grid[i] = list.get(i).toCharArray();
  80.         }
  81.        
  82.         return grid;
  83.     }
  84.    
  85.     public static void printGrid(char[][] grid){
  86.         for (int i = 0; i < grid.length; i++){
  87.             for (int j = 0; j < grid[0].length; j++){
  88.                 System.out.print(grid[i][j]);
  89.             }
  90.             System.out.println("");
  91.         }
  92.     }
  93.    
  94.     public static void printLine(char[] line){
  95.         for (char c : line){
  96.             System.out.print(c);
  97.         }
  98.         System.out.println("");
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement