Skip to content

feat: add typescript solution to lc problem: No.0079.Word Search #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion solution/0000-0099/0079.Word Search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

<!-- 这里可写通用的实现逻辑 -->

深度优先搜索 DFS 实现。
回溯(深度优先搜索 DFS 实现。

<!-- tabs:start -->

Expand Down Expand Up @@ -122,6 +122,47 @@ class Solution {
}
```

### **TypeScript**

```ts
function exist(board: string[][], word: string): boolean {
let m = board.length, n = board[0].length;
let visited = Array.from({ length: m }, v => new Array(n).fill(false));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (dfs(board, word, i, j, 0, visited)) {
return true;
}
}
}
return false;
};

function dfs(board: string[][], word: string, i: number, j: number, depth: number, visited: boolean[][]): boolean {
let m = board.length, n = board[0].length;
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || visited[i][j]) {
return false;
}
if (board[i][j] != word.charAt(depth)) {
return false;
}

if (depth == word.length - 1) {
return true;
}

visited[i][j] = true;
++depth;
let res = false;
for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) {
let x = i + dx, y = j + dy;
res = res || dfs(board, word, x, y, depth, visited);
}
visited[i][j] = false;
return res;
}
```

### **...**

```
Expand Down
41 changes: 41 additions & 0 deletions solution/0000-0099/0079.Word Search/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,47 @@ class Solution {
}
```

### **TypeScript**

```ts
function exist(board: string[][], word: string): boolean {
let m = board.length, n = board[0].length;
let visited = Array.from({ length: m }, v => new Array(n).fill(false));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (dfs(board, word, i, j, 0, visited)) {
return true;
}
}
}
return false;
};

function dfs(board: string[][], word: string, i: number, j: number, depth: number, visited: boolean[][]): boolean {
let m = board.length, n = board[0].length;
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || visited[i][j]) {
return false;
}
if (board[i][j] != word.charAt(depth)) {
return false;
}

if (depth == word.length - 1) {
return true;
}

visited[i][j] = true;
++depth;
let res = false;
for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) {
let x = i + dx, y = j + dy;
res = res || dfs(board, word, x, y, depth, visited);
}
visited[i][j] = false;
return res;
}
```

### **...**

```
Expand Down
36 changes: 36 additions & 0 deletions solution/0000-0099/0079.Word Search/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function exist(board: string[][], word: string): boolean {
let m = board.length, n = board[0].length;
let visited = Array.from({ length: m }, v => new Array(n).fill(false));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (dfs(board, word, i, j, 0, visited)) {
return true;
}
}
}
return false;
};

function dfs(board: string[][], word: string, i: number, j: number, depth: number, visited: boolean[][]): boolean {
let m = board.length, n = board[0].length;
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || visited[i][j]) {
return false;
}
if (board[i][j] != word.charAt(depth)) {
return false;
}

if (depth == word.length - 1) {
return true;
}

visited[i][j] = true;
++depth;
let res = false;
for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) {
let x = i + dx, y = j + dy;
res = res || dfs(board, word, x, y, depth, visited);
}
visited[i][j] = false;
return res;
}