Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank The Grid Search problem solution

YASH PAL, 31 July 202414 December 2025

In this HackerRank The Grid Search problem solution, 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. For example, consider the following grid:

1234567890  
0987654321  
1111111111  
1111111111  
2222222222  

The pattern array is:

876543  
111111  
111111

The pattern begins at the second row and the third column of the grid and continues in the following two rows. The pattern is said to be present in the grid. The return value should be YES or NO, depending on whether the pattern is found. In this case, return YES.

HackerRank The Grid Search problem solution

Hackerrank The Grid Search 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
    if not found:
        print ("NO")

The Grid search 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("YESn");
        else
            printf("NOn");
        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);
});

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes