Word Search II Hard / #212 / trie and grid together
There is a grid of letters and a list of words. Return every word that can be spelled by a path of cells touching up, down, left or right, with no cell used twice in one word. Our example: the grid tea / ons and the words toe, tea, ten — the answer is [ten, tea]; "toe" is not on the board.
input board: a grid of letters, m rows of n each; words: words, all different. returns Every word of words that can be read along a path of cells, each next one beside the one before it (above, below, left or right) and no cell used twice in the word. Each word once, in any order; [] if there is none. constraints 1 ≤ m = board.length ≤ 12 1 ≤ n = board[i].length ≤ 12 1 ≤ words.length ≤ 1000 1 ≤ words[i].length ≤ 10 board[i][j], words[i]: lowercase English letters the words of words are all different our 15 tests keep to them examples Open a test in the tests panel: it shows the input and the expected answer. walkthrough solve timer --:-- off
switch language → JavaScript TypeScript Python Go Java submit only: no test in the browserC++ submit only: no test in the browserother /**
* @param {character[][]} board
* @param {string[]} words
* @return {string[]}
*/
function findWords(board, words) {
}
test submit
15 tests · limit 3 s
01 — board = [["t", "e", "a"], ["o", "n", "s"]]
words = ["toe", "tea", "ten"] copy 02 — board = [["a", "b"], ["c", "d"]]
words = ["aba", "abdc", "acdb"] copy 03 — board = [["c", "a", "t", "s"]]
words = ["ca", "cat", "cats", "cast", "tac"] copy 04 — board = [["a", "b", "a"]]
words = ["ab", "aba", "bab"] copy 05 — board = [["b", "a", "a"], ["a", "a", "a"]]
words = ["aabaa", "aaaaa", "aaaaaa"] copy 06 — board = [["x"]]
words = ["x"] copy 07 — board = [["x"]]
words = ["y", "xx"] copy 08 — board = [["m", "z", "m", "o", "q", "d", "x", "c", "b", "i", "f", "j"], ["d", "k", "c", "p", "e", "j", "q", "r", "l", "p", "y", "b"], ["k", "a", "s", "m", "a", "g", "l", "t", "w", "x", "s", "p"], ["i", "t", "d", "i", "v", "p", "e", "l", "k", "m", "y", "d"], ["z", "w", "b", "k", "l", "m", "y", "r", "f", "k", "l", "s"], ["l", "u", "e", "g", "b", "a", "g", "m", "p", "x", "s", "l"], ["r", "f", "a", "g", "u", "z", "b", "v", "s", "n", "h", "v"], ["e", "h", "v", "v", "i", "n", "a", "j", "v", "p", "w", "d"], ["w", "f", "d", "n", "f", "c", "d", "g", "s", "k", "i", "j"], ["e", "y", "q", "h", "c", "z", "o", "s", "f", "n", "j", "m"], ["i", "v", "g", "f", "h", "r", "v", "p", "y", "z", "x", "x"], ["t", "h", "b", "r", "o", "j", "r", "p", "y", "u", "h", "r"]]
words = ["crl", "sc", "mzkatiz", "u", "cjaour", "xlesdd", "mieg", "nzoib", "uidjfxdch", "ofh", "adw", "eympvlkbea", … 1000 strings] copy 09 — board = [["b", "b", "b", "b", "b", "b", "b", "a", "b", "c", "b", "c"], ["c", "c", "a", "b", "c", "a", "b", "c", "a", "a", "c", "c"], ["a", "c", "c", "a", "a", "a", "c", "c", "a", "a", "b", "a"], ["c", "a", "c", "b", "a", "b", "b", "c", "a", "a", "a", "b"], ["c", "a", "c", "b", "b", "c", "b", "b", "c", "b", "c", "a"], ["a", "c", "b", "a", "a", "b", "b", "c", "a", "b", "b", "a"], ["a", "b", "c", "c", "b", "a", "a", "c", "c", "a", "c", "b"], ["b", "a", "a", "b", "a", "c", "c", "c", "c", "c", "a", "c"], ["b", "a", "c", "b", "b", "a", "a", "c", "a", "c", "b", "c"], ["b", "c", "b", "c", "b", "c", "b", "b", "c", "a", "b", "b"], ["c", "c", "c", "a", "a", "c", "b", "c", "b", "a", "a", "a"], ["c", "c", "b", "b", "b", "a", "b", "b", "c", "b", "c", "b"]]
words = ["ccbcc", "ccbacbc", "aabcbabbc", "cb", "accaaababa", "bbabab", "acbbaccccb", "caaacbc", "ccbbcbabab", "ab", "acbbaac", "cbbbaaa", … 273 strings] copy 10 — board = [["g", "b", "c", "d", "h", "e", "f", "a", "f", "g", "g", "g"]]
words = ["gbcdhefafg", "dgah", "cdhefa", "efafg", "ehdcb", "gfaf", "gbcdh", "cf", "gf", "fafggg", "ebefe", "ggg", … 25 strings] copy 11 — board = [["e"], ["f"], ["h"], ["c"], ["h"], ["h"], ["f"], ["c"], ["c"], ["a"], ["h"], ["b"]]
words = ["hchhfccahb", "ac", "hc", "fed", "caf", "bhacc", "bhaccfhhch", "cgf", "feahh", "gh", "cf", "ce", … 24 strings] copy 12 — board = [["b", "a", "b", "b"], ["b", "b", "a", "b"], ["b", "a", "a", "a"], ["b", "a", "b", "b"]]
words = ["bbbaababab", "bbbbbabbb", "baabbba", "bababbabab", "abbabba", "bababaaab", "aaabbabbb", "babbaa", "baaabbbbb", "abbbabb", "bbbabb", "babbabbb", … 79 strings] copy 13 — board = [["b", "b", "b", "a", "b", "b"], ["b", "a", "b", "a", "a", "b"], ["a", "a", "a", "a", "b", "a"], ["b", "b", "a", "a", "b", "a"], ["b", "b", "b", "b", "a", "a"], ["b", "a", "b", "b", "a", "a"]]
words = ["aababaabaa", "aaa", "aababab", "a", "aababaaba", "aababaabab", "ab", "aa", "aabab", "aababa", "aaba", "aababaab", … 14 strings] copy 14 — board = [["z", "z", "z", "z"], ["z", "z", "z", "z"], ["z", "z", "z", "z"]]
words = ["z", "zzzzzzzzz", "zzzzzzzzzz", "zzzzzz", "zzzzzzzz", "zzzzzzz", "zzzz", "zzz", "zz", "zzzzz"] copy 15 — board = [["c", "b", "c", "a", "a", "b", "d", "b"], ["b", "b", "b", "c", "c", "d", "c", "c"], ["d", "c", "d", "c", "c", "d", "d", "d"], ["d", "c", "a", "d", "a", "d", "c", "c"], ["d", "d", "a", "a", "b", "a", "b", "b"], ["b", "d", "a", "a", "b", "b", "b", "c"], ["c", "d", "c", "a", "b", "d", "b", "a"], ["d", "b", "d", "b", "d", "c", "c", "b"]]
words = ["cby", "cdz", "dbaaae", "bcccz", "bx", "dbcdccdz", "cdbdbaby", "bbcabcbdcy", "acdcdbe", "ccddy", "dcbddx", "ade", … 50 strings] copy
Stuck? See the solution run step by step on our example, then why it works and its complexity.
Watch the walkthrough → The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.