Sort by

recency

|

901 Discussions

|

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

  • + 0 comments

    Here is my solution in PHP

        $prows = count($P);
        $grows = count($G);
        $firstRow = array();
        $firstIndex = array();
        
        $result = 'NO';
        for ($i=0; $i <=$grows-($prows-1) ; $i++) { 
            $rowIndex = array();
            $offset = 0;
            while(($str = strpos($G[$i], $P[0],$offset))!== false)
            {
                $rowIndex[] = $str;
                $offset = $str+1;
    
            }
            $firstRow[$i] = $rowIndex;
        }
    
     
         foreach ($firstRow as $key => $value) {
             // code...
            if($result == 'YES')
            {
                break;
            }
            if($grows - ($key) >= $prows)
            {
                foreach ($value as $key1 => $value1) {
                     $keyIndex =  $value1;
                     $newKey = 0;
                     $lastIndex = $key+$prows-1;
                    
                          for ($i=$key+1; $i <($key+$prows) ; $i++) { 
                             
                          $newKey+=1;
                          
                          $str = strpos($G[$i], $P[$newKey],$value1);
                         
                    if ($str !== false && $str == $keyIndex) {
                            if($i == $lastIndex)
                             {
                               $result = 'YES';
                                break;
                             }
                    }
                   else
                   {
                    break;
                   }
    
                 
               }
               
                }
             
             
       
            }
            else
            {
                continue;
            }
         }    
     return $result;
    
  • + 0 comments

    never says it should be exactly under... reputation management experts it states if the patterns is found in the string... it is found, they are not at the same indexes except IPTV, but I didn't found that requirement in the specifications.

  • + 0 comments

    Here is problem solution in PYthon, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-the-grid-search-problem-solution.html