Skip to content

Commit 6200cb5

Browse files
committed
Add week 9 solutions: designAddAndSearchWordsDatatStructure, numberOfIslands
1 parent 6bd3ab1 commit 6200cb5

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// AddWord Method : Time Complexity: O(L)/Space Complexity: O(L)
2+
// Search Method : Time Complexity: O(M)/Space Complexity: O(1)
3+
// SearchInNode Method : Time Complexity: O(M)/Space Complexity: O(1)
4+
5+
var WordDictionary = function () {
6+
this.root = {};
7+
};
8+
9+
WordDictionary.prototype.addWord = function (word) {
10+
// start from the root node
11+
let node = this.root;
12+
for (let char of word) {
13+
if (!node[char]) {
14+
// if the character does not exist, create a new node
15+
node[char] = {};
16+
}
17+
// move to the next node
18+
node = node[char];
19+
}
20+
// mark the end of the word
21+
node.isEndOfWord = true;
22+
};
23+
24+
WordDictionary.prototype.search = function (word) {
25+
// start searching from the root
26+
return this.searchInNode(word, 0, this.root);
27+
};
28+
29+
WordDictionary.prototype.searchInNode = function (word, index, node) {
30+
if (index === word.length) {
31+
// if all characters are checked
32+
return node.isEndOfWord === true;
33+
}
34+
35+
const char = word[index];
36+
if (char === ".") {
37+
//iIf the character is '.', check all possible nodes at this part
38+
for (let key in node) {
39+
if (
40+
key !== "isEndOfWord" &&
41+
this.searchInNode(word, index + 1, node[key])
42+
) {
43+
// if any path matches, return true
44+
return true;
45+
}
46+
}
47+
// no path matched
48+
return false;
49+
} else if (node[char]) {
50+
// if the character exists in the trie
51+
return this.searchInNode(word, index + 1, node[char]);
52+
} else {
53+
// character path does not exist
54+
return false;
55+
}
56+
};

number-of-islands/yolophg.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Time Complexity: O(m * n)
2+
// Space Complexity: O(m * n)
3+
4+
var numIslands = function (grid) {
5+
const m = grid.length;
6+
const n = grid[0].length;
7+
let numIslands = 0;
8+
9+
// directions arrays for moving up, down, left, and right
10+
const directions = [
11+
[-1, 0],
12+
[1, 0],
13+
[0, -1],
14+
[0, 1],
15+
];
16+
17+
// BFS function
18+
function bfs(row, col) {
19+
const queue = [[row, col]];
20+
// mark as visited
21+
grid[row][col] = "0";
22+
23+
while (queue.length > 0) {
24+
const [r, c] = queue.shift();
25+
26+
for (const [dr, dc] of directions) {
27+
const nr = r + dr;
28+
const nc = c + dc;
29+
30+
if (nr >= 0 && nr < m && nc >= 0 && nc < n && grid[nr][nc] === "1") {
31+
// mark as visited
32+
grid[nr][nc] = "0";
33+
queue.push([nr, nc]);
34+
}
35+
}
36+
}
37+
}
38+
39+
// iterate through each cell in the grid
40+
for (let i = 0; i < m; i++) {
41+
for (let j = 0; j < n; j++) {
42+
if (grid[i][j] === "1") {
43+
numIslands++;
44+
// start BFS to mark all connected land cells
45+
bfs(i, j);
46+
}
47+
}
48+
}
49+
50+
return numIslands;
51+
};

0 commit comments

Comments
 (0)