|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# 212. Word Search II |
| 4 | +# https://leetcode.com/problems/word-search-ii |
| 5 | +# Hard |
| 6 | + |
| 7 | +=begin |
| 8 | +Given an m x n board of characters and a list of strings words, return all words on the board. |
| 9 | +
|
| 10 | +Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word. |
| 11 | +
|
| 12 | +Example 1: |
| 13 | +Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"] |
| 14 | +Output: ["eat","oath"] |
| 15 | +
|
| 16 | +Example 2: |
| 17 | +Input: board = [["a","b"],["c","d"]], words = ["abcb"] |
| 18 | +Output: [] |
| 19 | +
|
| 20 | +Constraints: |
| 21 | +* m == board.length |
| 22 | +* n == board[i].length |
| 23 | +* 1 <= m, n <= 12 |
| 24 | +* board[i][j] is a lowercase English letter. |
| 25 | +* 1 <= words.length <= 3 * 104 |
| 26 | +* 1 <= words[i].length <= 10 |
| 27 | +* words[i] consists of lowercase English letters. |
| 28 | +* All the strings of words are unique. |
| 29 | +=end |
| 30 | + |
| 31 | +# @param {Character[][]} board |
| 32 | +# @param {String[]} words |
| 33 | +# @return {String[]} |
| 34 | +def find_words(board, words) |
| 35 | + res = [] |
| 36 | + trie = {} |
| 37 | + |
| 38 | + words.each do |word| |
| 39 | + node = trie |
| 40 | + word.chars.each do |char| |
| 41 | + node[char] ||= {} |
| 42 | + node = node[char] |
| 43 | + end |
| 44 | + node[:word] = word |
| 45 | + end |
| 46 | + |
| 47 | + (0...board.size).each do |i| |
| 48 | + (0...board[0].size).each do |j| |
| 49 | + res += search(board, i, j, trie) |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + res |
| 54 | +end |
| 55 | + |
| 56 | +def search(board, i, j, node) |
| 57 | + res = [] |
| 58 | + |
| 59 | + char = board[i][j] |
| 60 | + char_node = node[char] |
| 61 | + |
| 62 | + return res if char_node.nil? || char_node.empty? |
| 63 | + |
| 64 | + if char_node.key?(:word) |
| 65 | + res << char_node[:word] |
| 66 | + char_node.delete(:word) |
| 67 | + if char_node.empty? |
| 68 | + node.delete(char) |
| 69 | + return res |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + board[i][j] = :visited |
| 74 | + [[0, 1], [0, -1], [1, 0], [-1, 0]].map { |di, dj| [di + i, dj + j] }.each do |next_i, next_j| |
| 75 | + next if next_i < 0 || next_j < 0 || next_i >= board.size || next_j >= board[0].size |
| 76 | + |
| 77 | + next_char = board[next_i][next_j] |
| 78 | + next if next_char == :visited |
| 79 | + |
| 80 | + res += search(board, next_i, next_j, char_node) |
| 81 | + if char_node.empty? |
| 82 | + node.delete(char) |
| 83 | + break |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + board[i][j] = char |
| 88 | + |
| 89 | + res |
| 90 | +end |
| 91 | + |
| 92 | + |
| 93 | +# **************** # |
| 94 | +# TEST # |
| 95 | +# **************** # |
| 96 | + |
| 97 | +require "test/unit" |
| 98 | +class Test_find_words < Test::Unit::TestCase |
| 99 | + def test_ |
| 100 | + assert_equal ["eat", "oath"].sort, find_words([["o", "a", "a", "n"], ["e", "t", "a", "e"], ["i", "h", "k", "r"], ["i", "f", "l", "v"]], ["oath", "pea", "eat", "rain"]).sort |
| 101 | + assert_equal [], find_words([["a", "b"], ["c", "d"]], ["abcb"]) |
| 102 | + end |
| 103 | +end |
0 commit comments