Skip to content

Commit fd45377

Browse files
Added 0009 Word Search II Problem + Made few changes to prev files
1 parent d72e3fd commit fd45377

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

L-A/0006 Wild Card Matching ( L-A )/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 0005 Trapping Rain Water ( L-A )
1+
# 0006 Wild Card Matching ( L-A )
22

33
## Problem
44

L-A/0007 The Skyline Problem/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 0005 Trapping Rain Water ( L-A )
1+
# 0007 The Skyline Problem ( L-A )
22

33
## Problem
44

L-A/0008 Longest Common Subsequence ( L-A )/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 0005 Longest Common Subsequence ( L-A )
1+
# 0008 Longest Common Subsequence ( L-A )
22

33
## Problem
44

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function findWords(board, words) {
2+
const result = []; // array to store found words
3+
4+
function dfs(i, j, word, visited) {
5+
// depth-first search function
6+
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length) return; // check if position is out of bounds
7+
if (visited[i][j]) return; // check if position has already been visited
8+
word += board[i][j]; // add current character to current word
9+
10+
if (words.includes(word)) {
11+
// check if current word is a target word
12+
result.push(word); // add word to result array
13+
}
14+
15+
visited[i][j] = true; // mark current position as visited
16+
17+
// search adjacent positions recursively
18+
dfs(i + 1, j, word, visited);
19+
dfs(i - 1, j, word, visited);
20+
dfs(i, j + 1, word, visited);
21+
dfs(i, j - 1, word, visited);
22+
23+
visited[i][j] = false; // mark current position as unvisited (backtrack)
24+
}
25+
26+
const visited = new Array(board.length)
27+
.fill(false)
28+
.map(() => new Array(board[0].length).fill(false)); // initialize visited array
29+
30+
for (let i = 0; i < board.length; i++) {
31+
// iterate over each position on the board
32+
for (let j = 0; j < board[0].length; j++) {
33+
dfs(i, j, "", visited); // perform depth-first search starting at current position
34+
}
35+
}
36+
37+
return result; // return array of found words
38+
}

0 commit comments

Comments
 (0)