This project is a simple implementation of a Word Search solver that checks whether a given word exists in a 2D grid (matrix) of letters.
✅ Traversal is allowed only in the four cardinal directions:
- Up
- Down
- Left
- Right
❌ Diagonal moves are not allowed.
❗ Words must be formed using consecutive letters, i.e., each letter in the word must be adjacent to the previous one.
### 📋 Example Board A B C D E F G H I J K L M N O P
✅ Output: true
(Path: F → I → N → E)
The solution uses Depth-First Search (DFS) recursively to:
- Start from each cell in the grid
- Check if the word can be constructed by moving up, down, left, or right step by step
- Track visited positions to avoid revisiting
Where:
N
is the number of rows in the boardM
is the number of columnsL
is the length of the word being searched
Why?
- In the worst case, we start DFS from every cell (
N * M
) - From each cell, we can explore up to 4 directions (
4^L
combinations in the depth of recursion for word of lengthL
)
- The recursion depth goes up to the length of the word
L
- Additionally, we use a
visited[][]
matrix of sizeN x M
(can also be reused)
So the total auxiliary space is:
- O(L) for recursion stack
- O(N*M) for the visited matrix (if not modifying the board in place) .