Check Each Word in a Character Grid
Updated
Problem statement
The grid board has m rows and n columns, and each cell holds one lowercase letter. You also get an array words of different words. For each word, decide whether it appears in the grid.
A word appears in the grid when the letters along some path of cells, read in order, spell it. The path may begin anywhere, and every step moves up, down, left or right by one cell. Within one word, a path may not visit the same cell twice. Each word is checked on its own, so two different words may use the same cells.
Return a boolean array answer of the same length as words, where answer[i] is true if words[i] appears in the grid and false if it cannot.
Function
checkWords(board: char[][], words: String[]) → boolean[]Examples
Example 1
board = [["c","a","t","s"],["o","r","e","d"],["w","a","l","k"]]words = ["care","rare","cow","stew"]return = [true,false,true,false]"care" follows the cells (0, 0), (0, 1), (1, 1), (1, 2): right, down, right. "rare" starts at (1, 1), the only r, and both a cells beside it lead back only to (1, 1), which this word has already used, so it is false. "cow" goes straight down the first column from (0, 0), using (0, 0) again; that is fine because each word is checked on its own. For "stew", the path (0, 3), (0, 2), (1, 2) works, but no w touches (1, 2), so it is false.
Example 2
board = [["a","b"],["c","d"]]words = ["abdc","acdb","ad"]return = [true,true,false]"abdc" goes right, down, left and "acdb" goes down, right, up, so both are true. In "ad", the letters a and d touch only diagonally, and a diagonal step is not allowed, so "ad" is false.
Example 3
board = [["z"]]words = ["z","zz"]return = [true,false]The board has one cell. "z" uses it once and is true; "zz" would need that cell twice, so it is false.
Constraints
m == board.lengthn == board[i].length1 <= m, n <= 12board[i][j]is a lowercase English letter.1 <= words.length <= 10001 <= words[i].length <= 10words[i]consists of lowercase English letters.- No word appears twice in
words.
More Zillow Onsite Interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
- Symmetric TreeONSITE INTERVIEW · Seen Oct 2020
- Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
- Uniform to Normal NumbersONSITE INTERVIEW · Seen Nov 2019
- Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
- Is SubstringONSITE INTERVIEW · Seen Nov 2017
Similar questions at other companies
- Boggle Word FinderBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Word Search IIAmazon · ONSITE INTERVIEW
- Word Search IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Enumerate Character Paths to a MarkerTesla · ONSITE INTERVIEWTesla interview questions
- Trace a Word Through a Letter GridExpedia · ONSITE INTERVIEWExpedia interview questions
- Word Search with Eight-Direction AdjacencyMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Word Search IIApple · PHONE SCREENApple interview questions
- Find a Word in a Letter GridAdobe · PHONE SCREENAdobe interview questions
- All Balanced Strings from Minimum Parenthesis InsertionsRubrik · ONSITE INTERVIEWRubrik interview questions
- Longest Increasing Path in a MatrixNew Relic · PHONE SCREEN · ONSITE INTERVIEW