31 · Backtracking

#79 Word Search

Mediumsolve on LeetCode ↗

frequently reported atUberBloombergByteDance / TikTokNetflixPayPalAmazon

the problem

In plain words

Given an m × n grid of letters and a word, return true if the word can be spelled by moving between horizontally or vertically adjacent cells, using each cell at most once.

in board = ["ABCE","SFCS","ADEE"], word = "ABCCED"

out true

in same board, word = "SEE"

out true

in same board, word = "ABCB"

out false

The only B next to C was already used.

  • 1 ≤ m, n ≤ 6
  • 1 ≤ word.length ≤ 15
  • Upper- and lowercase English letters

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #212
      Word Search II
      Hardin Tries
    2. #200
      Number of Islands
      Mediumin Graphs
    3. #51
      N-Queens
      Hardin Backtracking