Word Search in Grid
MediumP2Coding
grid is a rectangular grid of lowercase letters, given as an array of equal-length strings (one per row). Decide whether the word "masaischool" can be traced in it.
Start at any cell holding m, then repeatedly move to one of the up to 8 neighbouring cells (horizontal, vertical or diagonal) that holds the next letter. A cell may be used at most once in a single trace.
Example 1
- Input
- grid = ["masai","xxhcs","looxx"]
- Output
- true
- Why
- m, a, s, a, i run along the top row, then s, c, h, o, o, l wind back through the lower rows.
Example 2
- Input
- grid = ["abc","def"]
- Output
- false
- Why
- There is no `m` in the grid.
Example 3
- Input
- grid = ["m"]
- Output
- false
Constraints
- 1 <= grid.length, grid[i].length <= 10
- All rows have the same length and contain lowercase letters.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.