Skip to content

Commit

Permalink
feat: add js solution to lc problem: No.0079 (#3233)
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 authored Jul 9, 2024
1 parent 5b768cd commit b1292ee
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
36 changes: 36 additions & 0 deletions solution/0000-0099/0079.Word Search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,42 @@ function exist(board: string[][], word: string): boolean {
}
```

#### JavaScript

```js
function exist(board, word) {
const [m, n] = [board.length, board[0].length];
const dirs = [-1, 0, 1, 0, -1];
const dfs = (i, j, k) => {
if (k === word.length - 1) {
return board[i][j] === word[k];
}
if (board[i][j] !== word[k]) {
return false;
}
const c = board[i][j];
board[i][j] = '0';
for (let u = 0; u < 4; ++u) {
const [x, y] = [i + dirs[u], j + dirs[u + 1]];
const ok = x >= 0 && x < m && y >= 0 && y < n;
if (ok && board[x][y] !== '0' && dfs(x, y, k + 1)) {
return true;
}
}
board[i][j] = c;
return false;
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (dfs(i, j, 0)) {
return true;
}
}
}
return false;
}
```

#### Rust

```rust
Expand Down
36 changes: 36 additions & 0 deletions solution/0000-0099/0079.Word Search/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,42 @@ function exist(board: string[][], word: string): boolean {
}
```

#### JavaScript

```js
function exist(board, word) {
const [m, n] = [board.length, board[0].length];
const dirs = [-1, 0, 1, 0, -1];
const dfs = (i, j, k) => {
if (k === word.length - 1) {
return board[i][j] === word[k];
}
if (board[i][j] !== word[k]) {
return false;
}
const c = board[i][j];
board[i][j] = '0';
for (let u = 0; u < 4; ++u) {
const [x, y] = [i + dirs[u], j + dirs[u + 1]];
const ok = x >= 0 && x < m && y >= 0 && y < n;
if (ok && board[x][y] !== '0' && dfs(x, y, k + 1)) {
return true;
}
}
board[i][j] = c;
return false;
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (dfs(i, j, 0)) {
return true;
}
}
}
return false;
}
```

#### Rust

```rust
Expand Down
31 changes: 31 additions & 0 deletions solution/0000-0099/0079.Word Search/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function exist(board, word) {
const [m, n] = [board.length, board[0].length];
const dirs = [-1, 0, 1, 0, -1];
const dfs = (i, j, k) => {
if (k === word.length - 1) {
return board[i][j] === word[k];
}
if (board[i][j] !== word[k]) {
return false;
}
const c = board[i][j];
board[i][j] = '0';
for (let u = 0; u < 4; ++u) {
const [x, y] = [i + dirs[u], j + dirs[u + 1]];
const ok = x >= 0 && x < m && y >= 0 && y < n;
if (ok && board[x][y] !== '0' && dfs(x, y, k + 1)) {
return true;
}
}
board[i][j] = c;
return false;
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (dfs(i, j, 0)) {
return true;
}
}
}
return false;
}

0 comments on commit b1292ee

Please sign in to comment.