-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridSearch.cpp
More file actions
33 lines (28 loc) · 1.02 KB
/
GridSearch.cpp
File metadata and controls
33 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
https://www.hackerrank.com/challenges/the-grid-search/problem
This solution identifies if a given 2D grid exists within a bigger grid
*/
string gridSearch(vector<string> G, vector<string> P) {
int found=0;
// for each row in the bigger grid minus the height of smaller grid
for(int i = 0; i <= G.size() - P.size(); i++)
{
// for each column in the bigger grid minus the width of smaller grid
for(int j = 0; j <= G[0].size() - P[0].size(); j++)
{
int p = 0, k = i;
// if the row of smaller grid exists in bigger, check next row
while(G[k].substr(j, P[0].size()) == P[p].substr(0, P[0].size()))
{
// if we have reached a number of rows in G such that it is equal to smaller grid height, the pattern exists
if(k - i + 1 == P.size())
{
found=1;
break;
}
k++, p++;
}
}
}
return found ? "YES" : "NO";
}