Sort by

recency

|

904 Discussions

|

  • + 0 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

    I if check == True:
                    return "YES"
    return "NO"
    
  • + 0 comments

    Here's a One Line Python Magic

    def gridSearch(G, P):
        return "YES" if re.search(f".{{{len(G[0]) - len(P[0]) + 1}}}".join(P), " ".join(G)) else "NO"
    
  • + 1 comment

    import java.io.; import java.util.; import java.util.stream.*;

    class Result {

    public static String gridSearch(List<String> G, List<String> P) {
        int R = G.size();
        int C = G.get(0).length();
        int r = P.size();
        int c = P.get(0).length();
    
        for (int i = 0; i <= R - r; i++) {
            for (int j = 0; j <= C - c; j++) {
                boolean found = true;
                for (int k = 0; k < r; k++) {
                    if (!G.get(i + k).substring(j, j + c).equals(P.get(k))) {
                        found = false;
                        break;
                    }
                }
                if (found) return "YES";
            }
        }
        return "NO";
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int t = Integer.parseInt(bufferedReader.readLine().trim());
    
        for (int tItr = 0; tItr < t; tItr++) {
            String[] firstMultipleInput = bufferedReader.readLine().trim().split(" ");
            int R = Integer.parseInt(firstMultipleInput[0]);
            int C = Integer.parseInt(firstMultipleInput[1]);
    
            List<String> G = new ArrayList<>();
            for (int i = 0; i < R; i++) {
                G.add(bufferedReader.readLine());
            }
    
            String[] secondMultipleInput = bufferedReader.readLine().trim().split(" ");
            int r = Integer.parseInt(secondMultipleInput[0]);
            int c = Integer.parseInt(secondMultipleInput[1]);
    
            List<String> P = new ArrayList<>();
            for (int i = 0; i < r; i++) {
                P.add(bufferedReader.readLine());
            }
    
            String result = Result.gridSearch(G, P);
            bufferedWriter.write(result);
            bufferedWriter.newLine();
        }
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments

    Here my solution

    function gridSearch($G, $P) {
        // Write your code here
        $xG = count($G);
        $yG = strlen($G[0]);
        $xP = count($P);
        $yP = strlen($P[0]);
        
        for($x=0; $x<=$xG-$xP; $x++) {
            for ($y=0; $y<=$yG-$yP; $y++) {
                $matchLines = 0;
                for($i=0; $i<$xP; $i++) {
                    if(substr($G[$i+$x], $y, $yP) !== $P[$i]) break;
                    $matchLines++;
                }
                
                if($matchLines === $xP) return "YES";
            }
        }
        
        return "NO";
    }
    
  • + 0 comments

    Tip for C++ implementation: Use pass by reference for passing the grids. This will help clear the largest input, test case 5.