g0001_0100.s0079_word_search.complexity.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
**Time Complexity (Big O Time):**
1. The program uses a backtracking approach to explore all possible paths in the 2D board while searching for the target word.
2. The `exist` function iterates over all cells in the board, and for each cell, it invokes the `backtrace` function to start the backtracking process.
3. The `backtrace` function explores all four possible directions (up, down, left, right) from the current cell.
4. In the worst case, the `backtrace` function explores all possible paths in the board until it either finds the target word or determines that it cannot be formed.
5. The time complexity depends on the number of cells in the board (m * n), where 'm' is the number of rows, and 'n' is the number of columns. For each cell, the `backtrace` function explores four possible directions. Therefore, the overall time complexity is O(4^(m*n)), where 'm' and 'n' are the dimensions of the board.
**Space Complexity (Big O Space):**
1. The primary space usage in the program comes from the recursive call stack and the `visited` array.
2. The recursive call stack's maximum depth depends on the number of cells explored during backtracking. In the worst case, where the entire board is explored, the depth can be as large as 'm * n'. Therefore, the space complexity due to the call stack is O(m * n).
3. The `visited` array is used to keep track of visited cells to avoid revisiting them during the backtracking process. Its size is equal to the number of cells in the board, which is 'm * n'. Therefore, the space complexity due to the `visited` array is also O(m * n).
4. Combining the space complexities of the call stack and the `visited` array, the overall space complexity is O(m * n).
In summary, the time complexity of the program is O(4^(m*n)), and the space complexity is O(m * n), where 'm' and 'n' are the dimensions of the board. This program efficiently explores all possible paths in the board to search for the target word using a backtracking approach.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy