Last active
July 7, 2019 00:06
-
-
Save jubishop/41dbca2d1b83fa3daa7a26c63e096754 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def match(board, char, row, col) | |
return false if row < 0 or row >= board.length | |
return false if col < 0 or col >= board.first.length | |
return board[row][col] == char | |
end | |
def _exist(board, word, row, col) | |
return true if word.empty? | |
[[row-1, col], [row+1, col], [row, col-1], [row, col+1]].each { |cur_row, cur_col| | |
if (match(board, word[0], cur_row, cur_col)) | |
letter = board[cur_row][cur_col] | |
board[cur_row][cur_col] = nil | |
return true if (_exist(board, word[1..-1], cur_row, cur_col)) | |
board[cur_row][cur_col] = letter | |
end | |
} | |
return false | |
end | |
def exist(board, word) | |
return true if word.empty? | |
board.each_index { |row| | |
board[row].each_index { |col| | |
if (board[row][col] == word[0]) | |
letter = board[row][col] | |
board[row][col] = nil | |
return true if _exist(board, word[1..-1], row, col) | |
board[row][col] = letter | |
end | |
} | |
} | |
return false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment