当前位置:网站首页>79. Word search (medium string array matrix backtracking)
79. Word search (medium string array matrix backtracking)
2022-07-28 22:25:00 【Calm in the wind and rain】
79. Word search
Given a m x n Two dimensional character grid board And a string word word . If word Exists in the grid , return true ; otherwise , return false .
The words must be in alphabetical order , It's made up of letters in adjacent cells , among “ adjacent ” Cells are those adjacent horizontally or vertically . Letters in the same cell are not allowed to be reused .
Example 1:

Input :board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
Output :true
Example 2:

Input :board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “SEE”
Output :true
Example 3:

Input :board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCB”
Output :false
Tips :
m == board.length
n = board[i].length
1 <= m, n <= 6
1 <= word.length <= 15
board and word It consists of upper and lower case English letters only
Advanced : You can use search pruning techniques to optimize solutions , Make it board Larger situations can solve problems faster ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/word-search
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
backtracking . Need to consider marking letters , And trace back to delete the tag after use , In addition, you need to pay attention to how to get the return value .
Answer key (Java)
class Solution {
public boolean exist(char[][] board, String word) {
int m = board.length, n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
boolean flag = check(board, word, i, j, 0);
if (flag) {
return true;
}
}
}
return false;
}
private boolean check(char[][] board, String word, int i, int j, int k) {
if (board[i][j] != word.charAt(k)) {
return false;
} else if (k == word.length() - 1) {
return true;
}
char ch = board[i][j];
board[i][j] = '0';// Mark directly on the original array
int[][] dirs = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};// Auxiliary array moving up, down, left and right
boolean result = false;
for (int[] dir : dirs) {
int row = i + dir[0], col = j + dir[1];
if (row >= 0 && row < board.length && col >= 0 && col < board[0].length) {
if (board[row][col] != '0') {
boolean flag = check(board, word, row, col, k + 1);
if (flag) {
// flag by true Indicates that the end of the string has been reached
result = true;
break;
}
}
}
}
board[i][j] = ch;// Restore array
return result;
}
}
边栏推荐
- Openresty request authentication
- 40. Combined sum II
- 使用百度EasyDL实现明厨亮灶厨师帽识别
- What testing services do third-party software testing institutions provide? Charging standard of software test report
- What does GPRS network mean
- internet的基本服务中文件传输命令是哪个
- [Ruiji takeout project]day4 - dish management
- SQL注入 Less42(POST型堆叠注入)
- Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
- MySQL built-in functions
猜你喜欢

Basic introduction of Rockwell AB PLC rslogix digital quantity IO module

Jmeter 安装第三方插件 Plugins Manager

Win11 how to open software notification

成立不到一年!MIT衍生量子计算公司完成900万美元融资

What are the main functions and uses of LCR tester

Lvs+keepalived high availability deployment practical application

Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
![[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login](/img/53/c578e0d1428ea569fb412a20019924.png)
[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login

Netease Yunxin 2022q2 product supply station, come and get your product supply plan!

Hcip experiment (15)
随机推荐
普源示波器实际的使用效果怎么样
TensorFlow Serving 高性能的机器学习模型服务系统
Ordinary practice of JS DOM programming
openresty 请求鉴权
HCIP(13)
How to establish a decentralized community in Web3
Win11 how to open software notification
示波器发展史中的变化
SQL injection less42 (post stack injection)
【二叉树】二叉树中的伪回文路径
记录Flutter解决A RenderFlex overflowed by 7.3 pixels on the bottom溢出问题
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
Mysql内置函数
使用webWorker执行后台任务
Aimbetter insight into your database, DPM and APM solutions
Ruiji takeout project - development of business development function Day2
C语言编程规范学习笔记和总结
Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
Netease Yunxin 2022q2 product supply station, come and get your product supply plan!
SQL injection less34 (post wide byte injection + Boolean blind injection)