Blind 75
41 / 75Free practice

Word Search

Medium · Backtracking

Jump to code

Determine whether a nonempty word can be traced in a rectangular character board by horizontal or vertical moves, without using a cell twice in that path.

Examples

Example 1

board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
word = "ABCCED"

Output: true

Example 2

board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
word = "SEE"

Output: true

Constraints

  • 1 <= board.length, board[i].length <= 5
  • 1 <= word.length <= 10
  • board and word consists of only lowercase and uppercase English letters.
Need a hint?

A visited mark belongs to the current path, not to the entire search.

Real backtracking interview questions

Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 9 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    [
      "A",
      "B",
      "C",
      "E"
    ],
    [
      "S",
      "F",
      "C",
      "S"
    ],
    [
      "A",
      "D",
      "E",
      "E"
    ]
  ],
  "ABCCED"
]
Expected
true
Case 2
Input
[
  [
    [
      "A",
      "B",
      "C",
      "E"
    ],
    [
      "S",
      "F",
      "C",
      "S"
    ],
    [
      "A",
      "D",
      "E",
      "E"
    ]
  ],
  "SEE"
]
Expected
true
Case 3
Input
[
  [
    [
      "A",
      "B",
      "C",
      "E"
    ],
    [
      "S",
      "F",
      "C",
      "S"
    ],
    [
      "A",
      "D",
      "E",
      "E"
    ]
  ],
  "ABCB"
]
Expected
false
Case 4
Input
[
  [
    [
      "A"
    ]
  ],
  "A"
]
Expected
true
Case 5
Input
[
  [
    [
      "A"
    ]
  ],
  "B"
]
Expected
false

Drafts are saved per language in this browser when storage is available.