Skip to content

Instantly share code, notes, and snippets.

@mikestratton
Created March 4, 2022 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikestratton/ae6a2c8aab9360a6302ee91540028b9b to your computer and use it in GitHub Desktop.
Save mikestratton/ae6a2c8aab9360a6302ee91540028b9b to your computer and use it in GitHub Desktop.
Incomplete solution for Leetcode Word Search
<?php
// https://leetcode.com/problems/word-search/
function exist($board, $word) {
// count width
$x = count($board[0]);
// count height
$y = 0;
foreach($board as $ignored){
++$y;
}
// split & count word
$split = str_split($word);
$w = count($split) - 1;
for($i=0; $i<$x-1; $i++) {
for($j=0; $j <= $y; $j++) {
for($k=0; $k<$w; $k++){
if($split[$k] === $board[$i][$j]){
echo $split[$k] . '=' . $board[$i][$j] . ' located at xy: ' . $i . $j . '<br>';
}
}
}
}
}
$board = [
["A","B","C","E"],
["S","F","C","S"],
["A","D","E","E"]
];
$word = "ABCB";
exist($board, $word);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment