We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is my solution in Python:
def gridSearch(G, P):
R,C = len(G),len(G[0])
r,c = len(P),len(P[0])
for i in range(C-c+1):
for j in range(R-r+1):
#If the first element is correct, start to check:
if G[j][i] == P[0][0]:
check = True
for k in range(r):
for l in range(c):
if G[j+k][i+l] != P[k][l]:
check = False
if check == False:
break
I don't know a smarter way for this problem so I just brute force
I if check == True:
return "YES"
return "NO"
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Grid Search
You are viewing a single comment's thread. Return to all comments →
Here is my solution in Python: def gridSearch(G, P): R,C = len(G),len(G[0]) r,c = len(P),len(P[0]) for i in range(C-c+1): for j in range(R-r+1): #If the first element is correct, start to check: if G[j][i] == P[0][0]: check = True for k in range(r): for l in range(c): if G[j+k][i+l] != P[k][l]: check = False if check == False: break I don't know a smarter way for this problem so I just brute force