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.
The Grid Search
The Grid Search
+ 0 comments C#
public static string gridSearch(List<string> G, List<string> P) { int i = 0; int j = 0; int max = P.Max(x => x.Count()); while (P.Count() + i <= G.Count()) { while (j + max <= G[0].Length) { var matches = 0; for (int k = 0; k < P.Count(); ++k) { var str = G[k + i].Substring ( j, P[k].Count() ); if (!str.Equals(P[k])) break; ++matches; } if (matches == P.Count()) return "YES"; ++j; } j = 0; ++i; } return "NO"; }
+ 0 comments def gridSearch(G, P): # Write your code here R,C=len(G),len(G[0]) r,c=len(P),len(P[0]) #Iterate through all subgrids of (r,c) possible in G for i in range(R-r+1): for j in range(C-c+1): #Start comparing the elements of current subgrid for s in range(r): breakk=False for t in range(c): if G[i+s][j+t]!=P[s][t]: breakk=True break if breakk==True: break if breakk==False: return 'YES' return 'NO'
+ 0 comments Typescript
function gridSearch(G: string[], P: string[]): string { const gR = G.length const gC = G[0].length const pR = P.length const pC = P[0].length if(pR > gR || pC > gC){ return 'NO' } for(let i=0;i<=gR-pR;i++){ for(let j=0;j<=gC-pC;j++){ let fail = false for(let k=0;k<pR && !fail;k++){ for(let m=0;m<pC && !fail;m++){ if(G[i+k].charAt(j+m) !== P[k].charAt(m)){ fail = true } } } if(!fail){ return 'YES' } } } return 'NO' }
+ 0 comments public static String gridSearch(List<String> g, List<String> p) { for(int r = 0; r <= g.size() - p.size(); r++) { for(int c = 0; c <= g.get(r).length() - p.get(0).length(); c++) { boolean success = true; for (int i = 0; i < p.size() && success; i++) { for (int j = 0; j < p.get(i).length() && success; j++) { if (g.get(r + i).charAt(c + j) != p.get(i).charAt(j)) { success = false; } } } if (success) return "YES"; } } return "NO"; }
+ 0 comments Java8
public static String gridSearch(List<String> G, List<String> P) { for (int gy = 0; gy <= G.size() - P.size(); gy++) { for (int gx = 0; gx <= G.get(0).length() - P.get(0).length(); gx++) { int ok = 1; for (int py = 0; py < P.size() && ok == 1; py++) { for (int px = 0; px < P.get(0).length(); px++) { if (G.get(gy + py).charAt(gx + px) != P.get(py).charAt(px)) { ok = 0; break; } } } if (ok == 1) return "YES"; } } return "NO"; }
Load more conversations
Sort 852 Discussions, By:
Please Login in order to post a comment