Sort by

recency

|

910 Discussions

|

  • + 0 comments

    I really like the grid search. And I am using it from last 2 years.

  • + 0 comments

    Python3 Solution

    def isPatternExists(rowG, colG, G, P):
        for i in range(len(P)):
            for j in range(len(P[0])):
                if G[rowG+i][colG+j] != P[i][j]:
                    return False
        
        return True
                
    
    def gridSearch(G, P):
        rowP, colP = len(P), len(P[0])
        rowG, colG = len(G), len(G[0])
        
        for i in range(rowG):
            for j in range(colG):
                if i+rowP-1 >= rowG  or j+colP-1 >= colG:
                    break
                upperleft, upperright, lowerleft, lowerright = G[i][j], G[i][j+colP-1], G[i+rowP-1][j], G[i+rowP-1][j+colP-1]
    
                if upperleft == P[0][0] and upperright == P[0][colP-1] and lowerleft == P[rowP-1][0] and lowerright == P[rowP-1][colP-1]:
                    if isPatternExists(i, j, G, P):
                        return "YES"
        
        return "NO"
    
  • + 0 comments

    I dont know why i did this, used Q for poping left in case of big repeated patterns in single row

    def gridSearch(G, P):
        # Write your code here
        p_row = 0
        start_indexes = None
        row = 0
        while row < len(G):
            indexes = [i for i in range(len(G[row])) if G[row].startswith(P[p_row], i)]
            if indexes:
                if p_row == 0:
                    p_row+=1
                    start_indexes = deque(indexes)
                else:
                    size = len(start_indexes)
                    for _ in range(size):
                        current_index = start_indexes.popleft()
                        if current_index in indexes:
                            start_indexes.append(current_index)
                    if start_indexes:
                        p_row +=1
                    else:
                        p_row = 0
                        start_indexes = None            
            else:
                p_row = 1
                start_indexes = deque([i for i in range(len(G[row])) if G[row].startswith(P[0], i)])
            if p_row == len(P):
                return "YES"
            row+=1
        return "NO"
    
  • + 0 comments

    Here is problem solution in python, java, c++, c and javascript programming - https://programmingoneonone.com/hackerrank-the-grid-search-problem-solution.html

  • + 0 comments

    c++ solution - pretty ugly (i tried):

    size_t check(vector<string> & G, vector<string> & P, int indexG, int indexP, size_t indexFind) {
        for (int indexLineP = 1; indexLineP < P.size(); indexLineP++) {
            string & refGrid = G.at(indexG + indexLineP);
            string & refPattern = P.at(indexLineP);
            
            for (int stringIndex = indexFind + 0; stringIndex < indexFind + refPattern.size(); stringIndex++) {
                if (refGrid.at(stringIndex) != refPattern.at(stringIndex - indexFind)) {
                    return string::npos;
                }
            }
        }
        
        return 0;
    }
    
    string gridSearch(vector<string> G, vector<string> P) {
        for (int indexLineG = 0; indexLineG < G.size() - P.size() + 1; indexLineG++) {
            string & refGrid = G.at(indexLineG);
            string & refPattern = P.at(0);
    
            size_t indexFind = refGrid.find(refPattern);
            while (indexFind != string::npos) {
                if (check(G, P, indexLineG, 0, indexFind) != string::npos) {
                    return "YES";
                }
                indexFind ++;
                indexFind = refGrid.find(refPattern, indexFind);
            }
        }
        
        return "NO";        
    }