-
Notifications
You must be signed in to change notification settings - Fork 0
The Grid Search
Moustafa Attia edited this page Apr 2, 2018
·
1 revision
This wiki to show my solution for HackerRank problem The Grid Search
This type of problems can be solved in straight-forward solution, you just need to take care of edge cases. NOTE: No need to convert grid into [int][int], i will keep it grid of strings.
- iterate over all rows of grid
- check if first row of pattern exist
- if exist, check that it’s not at the end of column and not at the bottom of rows
- make inner loop from 1 to number of pattern rows
- check that all the remaining pattern rows are matching with same column index as starting index
string gridSearch(vector <string> G, vector <string> P, int c) {
// G = input grid
// P = input pattern
// c = pattern column, can be replaced with P[0].size()
for (int x = 0; x < G.size(); x++) {
for (int i = 0; i < G[x].size() - c + 1; i++){
if (G[x].substr(i, c) == P[0]) {
if (P.size() == 1){
return "YES";
}
if (i + c > G[x].size()) {
return "NO";
}
if (x + P.size() > G.size()){
return "NO";
}
int r = 1;
int y = x + 1;
for (int j = 1; j < P.size(); j++){
if (G[y].substr(i, c) == P[j]){
r++;
}
y++;
}
if (r == P.size()){
return "YES";
}
}
}
}
return "NO";
}