Sort by

recency

|

892 Discussions

|

  • + 0 comments

    Here is my Python Code:

    def gridSearch(G, P):
        # Write your code here
        
        rows_number, column_number = len(G), len(G[0])
        patterns_number, first_pattern_len = len(P), len(P[0])
        
        if patterns_number > rows_number or first_pattern_len > column_number:
            return "NO"
        
        for row_i in range(rows_number):
            
            rows_left = rows_number - row_i
            
            if rows_left < patterns_number:
                return "NO"
                        
            for col_j in range(column_number - first_pattern_len + 1) :
               
                if G[row_i][col_j:col_j + first_pattern_len] == P[0]:
                    
                    matches = [
                        G[row_i + i][col_j:col_j + first_pattern_len] == P[i]
                        for i in range(patterns_number)
                    ]
                    
                    if all(matches):
                        return "YES"
        return "NO"
    
  • + 0 comments

    Here is my Python code! It's probably not the most efficient but I couldn't find any faster algorithms.

    def gridSearch(G, P):
        for row in range(len(G) - len(P) + 1):
            for col in range(len(G[0]) - len(P[0]) + 1):
                if G[row][col:col + len(P[0])] == P[0]:
                    found = True
                    for nrow in range(1, len(P)):
                        if G[row + nrow][col: col + len(P[0])] != P[nrow]:
                            found = False
                            break
                    if found:
                        return "YES"
        return "NO"
    
  • + 0 comments

    Python3, not the most clever solution. I just check if there's a pattern in G starting at index (i,j).

    def is_grid(G, P, g_i, g_j):
        p_m, p_n = len(P[0]), len(P)
        g_m, g_n = len(G[0]), len(G)
        
        if (g_i+p_n > g_n) or (g_j+p_m > g_m):
            return False
        
        for i in range(p_n):
            for j in range(p_m):
                if G[g_i+i][g_j+j] != P[i][j]:
                    return False
        return True
    
    
    def gridSearch(G, P):
        g_m, g_n = len(G[0]), len(G)
        
        for i in range(g_n):
            for j in range(g_m):
                if is_grid(G, P, i, j):
                    return 'YES'
        return 'NO'
    
  • + 0 comments

    C++ Solution

    string gridSearch(vector<string> G, vector<string> P) {
      bool found = false;
      for(int i = 0; i < G.size(); i++) {
        int ind = G[i].find(P[0]);
        while(ind != string::npos) {
          found = true;
          for(int j = 1; j < P.size(); j++){
            if(i+j >= G.size() || G[i+j].find(P[j], ind) != ind) {
              found = false;
              break;
            }
          }
          if(found)
            return "YES";
          ind = G[i].find(P[0], ind+1);
        }
      }
      return "NO";
    }
    
  • + 3 comments

    Please, could anybody explain me why test case 6 having the data

    1
    4 4
    1234
    4321
    9999
    9999
    2 2
    12
    21
    

    Expecting Output == NO ?

    The first line has number 12 a the second line has number 21.