|
| 1 | +# 0009 Word Search II ( L-A ) |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +Given two strings, find the length of their longest common subsequence (LCS). A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. |
| 6 | + |
| 7 | +## Example 1 |
| 8 | + |
| 9 | +``` |
| 10 | +Input: |
| 11 | +str1 = "ABCDGH" |
| 12 | +str2 = "AEDFHR" |
| 13 | +
|
| 14 | +Output: |
| 15 | +The longest common subsequence is "ADH" with a length of 3. |
| 16 | +
|
| 17 | +Input: |
| 18 | +str1 = "AGGTAB" |
| 19 | +str2 = "GXTXAYB" |
| 20 | +
|
| 21 | +Output: |
| 22 | +The longest common subsequence is "GTAB" with a length of 4. |
| 23 | +``` |
| 24 | + |
| 25 | +## Solution Pseudocode |
| 26 | + |
| 27 | +```javascript |
| 28 | +function findWords(board, words): |
| 29 | + result = [] |
| 30 | + |
| 31 | + function dfs(i, j, word, visited): |
| 32 | + if i < 0 or j < 0 or i >= board.length or j >= board[0].length: |
| 33 | + return |
| 34 | + if visited[i][j]: |
| 35 | + return |
| 36 | + word += board[i][j] |
| 37 | + |
| 38 | + if words includes word: |
| 39 | + result.push(word) |
| 40 | + |
| 41 | + visited[i][j] = true |
| 42 | + |
| 43 | + dfs(i+1, j, word, visited) |
| 44 | + dfs(i-1, j, word, visited) |
| 45 | + dfs(i, j+1, word, visited) |
| 46 | + dfs(i, j-1, word, visited) |
| 47 | + |
| 48 | + visited[i][j] = false |
| 49 | + |
| 50 | + visited = new Array(board.length).fill(false).map(() => new Array(board[0].length).fill(false)) |
| 51 | + |
| 52 | + for i from 0 to board.length-1: |
| 53 | + for j from 0 to board[0].length-1: |
| 54 | + dfs(i, j, "", visited) |
| 55 | + |
| 56 | + return result |
| 57 | +``` |
| 58 | + |
| 59 | +## How it works |
| 60 | + |
| 61 | +- We first define a `result` array to store the found words. Then, we define a `dfs` function that takes the current position on the board (`i` and `j`), the current word being built (`word`), and a `visited` array to keep track of visited positions. |
| 62 | +- The function checks if the current position is out of bounds or has already been visited. If either of these conditions are true, the function returns. Otherwise, the function adds the current character to the word, and checks if the word is in the list of target words. If it is, it adds the word to the `result` array. |
| 63 | +- The function then marks the current position as visited and performs a depth-first search on all adjacent positions. After the search is complete, the current position is marked as unvisited. |
| 64 | +- Lastly, we create a `visited` array with the same dimensions as the board, and iterate through each position on the board, calling the `dfs` function with the initial parameters. The function returns the `result` array containing all the found words. |
| 65 | + |
| 66 | +## References |
| 67 | + |
| 68 | +- [LeetCode](https://leetcode.com/problems/word-search-ii/) |
| 69 | + |
| 70 | +## Problem Added By |
| 71 | + |
| 72 | +- [Haris](https://github.com/harisdev-netizen) |
| 73 | + |
| 74 | +## Contributing |
| 75 | + |
| 76 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 77 | + |
| 78 | +Please make sure to update tests as appropriate. |
0 commit comments