# HackerRank The Grid Search problem solution

In this HackerRank The Grid Search problem, you have Given an array of strings of digits, try to find the occurrence of a given pattern of digits. In the grid and pattern arrays, each string represents a row in the grid.

## Problem solution in Python programming.

```tests = int(input())
n = 0
n2 = 0
for i in range(0, tests):
inp = input()
inp = inp.split(' ')
n = int(inp[0])
thegrid = []
for i in range(0, n):
s = input()
thegrid.append(s)
inp2 = input()
inp2 = inp2.split(' ')
n2 = int(inp2[0])
subgrid = []
for i in range(0, n2):
s = input()
subgrid.append(s)
found = False
for i in range(0, len(thegrid)):
for j in range(0, len(subgrid)):
if(subgrid[j] in thegrid[i + j]):
if (j == len(subgrid)-1):
found = True
print ("YES")
break
continue
else:
break
break
print ("NO")```

## Problem solution in Java Programming.

```import java.io.*;
import java.util.*;

public class Solution {

public static boolean isMatch(String[] grid, int r, int c, String[] pattern) {
for(int i = r; i < r + pattern.length; i++) {
if(!grid[i].substring(c, c + pattern[0].length()).equals(pattern[i - r]))
return false;
}
return true;
}

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your clas should be named Solution. */
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int k = 0; k < T; k++){
int R = sc.nextInt();
int C = sc.nextInt();
String[] grid = new String[R];
for(int i = 0; i < R; i++){
grid[i] = sc.next();
}
int r = sc.nextInt();
int c = sc.nextInt();
String[] pattern = new String[r];
for(int i = 0; i < r; i++) {
pattern[i] = sc.next();
}
boolean ret = false;
for(int i = 0; i <= R - r; i++){
for(int j = 0; j <= C - c; j++){
if(grid[i].charAt(j) == pattern[0].charAt(0)){
ret = isMatch(grid, i, j, pattern);
if(ret)
break;
}
}
if(ret)
break;
}
if(ret){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
}```

### Problem solution in C++ programming.

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
char arr[1001][1001];
char find2[1001][1001];
int main() {
int a; cin >> a;
for (int g=0;g<a; g++)
{
int b,c;cin>>b>>c;
for (int g=0;g<b; g++)
{
for (int y=0;y<c; y++)
{
cin >> arr[g][y];
}
}
int d,e;cin>>d>>e;
for (int g=0;g<d; g++)
{
for (int y=0;y<e; y++)
{
cin >> find2[g][y];
}
}
if (d>b || e>c)
{
cout << "NO" << '\n'; continue;
}int realcheck=0;
for (int z=0;z<=b-d; z++)
{
for (int zz=0; zz<=c-e; zz++)
{int checker=0;
for (int l=z; l<z+d; l++)
{
for (int ll=zz; ll<zz+e; ll++)
{
if (find2[l-z][ll-zz]!=arr[l][ll])
{
checker=1; break;
}
}if (checker==1)break;
}
if (checker==0) {realcheck=1; break;}
}if (realcheck==1) break;
}
if (realcheck) cout << "YES" << '\n';
else cout << "NO" << '\n';
}
return 0;
}```

### Problem solution in C programming.

```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int is_match(char** a,int r, int c,char **pat,int pr,int pc,int starty,int startx){
int i,j;
if(pr+starty>r||pc+startx>c)
return 0;
for(i=0;i<pr;i++){
for(j=0;j<pc;j++){
if(a[starty+i][startx+j]!=pat[i][j]){
return 0;
}
}
}
return 1;
}
int main() {
int ntest;
scanf("%i",&ntest);
int s;
int found=0;
for(s=0;s<ntest;s++){
int r,c;
scanf("%i %i",&r,&c);
char** test;
test=malloc(sizeof(char*)*r);
int i;
for(i=0;i<r;i++){
test[i]=calloc(sizeof(char),1+c);
scanf("%s",test[i]);
}
int R,C;
scanf("%i %i",&R,&C);

char **pattern;
pattern=malloc(sizeof(char*)*R);
for(i=0;i<R;i++){
pattern[i]=calloc(sizeof(char),1+C);
scanf("%s",pattern[i]);
}

for(i=0;i<r;i++){
int j;
for(j=0;j<c;j++){
if(is_match(test,r,c,pattern,R,C,i,j))
{
found=1;
goto quit;
}
}
}
quit:
if(found)
printf("YES\n");
else
printf("NO\n");
found=0;
for(i=0;i<R||i<r;i++){
if(i<R)
free(pattern[i]);
free(test[i]);

}

}

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}```

### Problem solution in JavaScript programming.

```function processData(input) {
var lines = input.split('\n'),
nCases = parseInt(lines[0]);
var contains = function(gridStart, gridRows, patternStart, patternRows) {
var stopIndex = gridStart + gridRows - patternRows + 1,
innerStopIndex = patternStart + patternRows;
var cmpIdx;
for(var i = gridStart; i < stopIndex; i++) {
cmpIdx = lines[i].indexOf(lines[patternStart]);
if(cmpIdx > -1) {
for(var j = i + 1, k = patternStart + 1; k < innerStopIndex && cmpIdx === lines[j].indexOf(lines[k]); ++j,++k);
if(k === innerStopIndex)
return true;
}
}
return false;
};
var i = 1,
gridRows = parseInt(lines[i].split(' ')[0]),
gridStart = i + 1,
j = i + gridRows + 1,
patternRows = parseInt(lines[j].split(' ')[0]),
patternStart = j + 1;
do {
if(contains(gridStart, gridRows, patternStart, patternRows))
console.log('YES');
else
console.log('NO');
i = patternStart + patternRows;
gridRows = parseInt(lines[i].split(' ')[0]);
gridStart = i + 1;
j = i + gridRows + 1;
patternRows = parseInt(lines[j].split(' ')[0]);
patternStart = j + 1;
nCases--;
} while(nCases > 0)
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});```