Skip to content

Commit 9f8ed6d

Browse files
authored
feat: add typescript solution to lc problem: No.0079.Word Search (doocs#474)
1 parent dafa769 commit 9f8ed6d

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

solution/0000-0099/0079.Word Search/README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

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

57-
深度优先搜索 DFS 实现。
57+
回溯(深度优先搜索 DFS 实现。
5858

5959
<!-- tabs:start -->
6060

@@ -122,6 +122,47 @@ class Solution {
122122
}
123123
```
124124

125+
### **TypeScript**
126+
127+
```ts
128+
function exist(board: string[][], word: string): boolean {
129+
let m = board.length, n = board[0].length;
130+
let visited = Array.from({ length: m }, v => new Array(n).fill(false));
131+
for (let i = 0; i < m; ++i) {
132+
for (let j = 0; j < n; ++j) {
133+
if (dfs(board, word, i, j, 0, visited)) {
134+
return true;
135+
}
136+
}
137+
}
138+
return false;
139+
};
140+
141+
function dfs(board: string[][], word: string, i: number, j: number, depth: number, visited: boolean[][]): boolean {
142+
let m = board.length, n = board[0].length;
143+
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || visited[i][j]) {
144+
return false;
145+
}
146+
if (board[i][j] != word.charAt(depth)) {
147+
return false;
148+
}
149+
150+
if (depth == word.length - 1) {
151+
return true;
152+
}
153+
154+
visited[i][j] = true;
155+
++depth;
156+
let res = false;
157+
for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) {
158+
let x = i + dx, y = j + dy;
159+
res = res || dfs(board, word, x, y, depth, visited);
160+
}
161+
visited[i][j] = false;
162+
return res;
163+
}
164+
```
165+
125166
### **...**
126167

127168
```

solution/0000-0099/0079.Word Search/README_EN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,47 @@ class Solution {
109109
}
110110
```
111111

112+
### **TypeScript**
113+
114+
```ts
115+
function exist(board: string[][], word: string): boolean {
116+
let m = board.length, n = board[0].length;
117+
let visited = Array.from({ length: m }, v => new Array(n).fill(false));
118+
for (let i = 0; i < m; ++i) {
119+
for (let j = 0; j < n; ++j) {
120+
if (dfs(board, word, i, j, 0, visited)) {
121+
return true;
122+
}
123+
}
124+
}
125+
return false;
126+
};
127+
128+
function dfs(board: string[][], word: string, i: number, j: number, depth: number, visited: boolean[][]): boolean {
129+
let m = board.length, n = board[0].length;
130+
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || visited[i][j]) {
131+
return false;
132+
}
133+
if (board[i][j] != word.charAt(depth)) {
134+
return false;
135+
}
136+
137+
if (depth == word.length - 1) {
138+
return true;
139+
}
140+
141+
visited[i][j] = true;
142+
++depth;
143+
let res = false;
144+
for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) {
145+
let x = i + dx, y = j + dy;
146+
res = res || dfs(board, word, x, y, depth, visited);
147+
}
148+
visited[i][j] = false;
149+
return res;
150+
}
151+
```
152+
112153
### **...**
113154

114155
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function exist(board: string[][], word: string): boolean {
2+
let m = board.length, n = board[0].length;
3+
let visited = Array.from({ length: m }, v => new Array(n).fill(false));
4+
for (let i = 0; i < m; ++i) {
5+
for (let j = 0; j < n; ++j) {
6+
if (dfs(board, word, i, j, 0, visited)) {
7+
return true;
8+
}
9+
}
10+
}
11+
return false;
12+
};
13+
14+
function dfs(board: string[][], word: string, i: number, j: number, depth: number, visited: boolean[][]): boolean {
15+
let m = board.length, n = board[0].length;
16+
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || visited[i][j]) {
17+
return false;
18+
}
19+
if (board[i][j] != word.charAt(depth)) {
20+
return false;
21+
}
22+
23+
if (depth == word.length - 1) {
24+
return true;
25+
}
26+
27+
visited[i][j] = true;
28+
++depth;
29+
let res = false;
30+
for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) {
31+
let x = i + dx, y = j + dy;
32+
res = res || dfs(board, word, x, y, depth, visited);
33+
}
34+
visited[i][j] = false;
35+
return res;
36+
}

0 commit comments

Comments
 (0)