Word Search
Detailed guide and Python implementation for the 'Word Search' problem.
Problem Statement
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Implement a function exist(board: list, word: str) -> bool.
- •m == board.length
- •n == board[i].length
- •1 <= m, n <= 6
- •1 <= word.length <= 15
- •board and word consists of only lowercase and uppercase English letters
Examples
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED"
True
The word ABCCED can be traced: A(0,0)->B(0,1)->C(0,2)->C(1,2)->E(2,2)->D(2,1).
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE"
True
The word SEE can be traced: S(1,3)->E(2,3)->E(2,2).
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB"
False
Cannot trace ABCB without reusing cells.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.