当前位置:网站首页>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;
}
}
边栏推荐
- Learning notes and summary of C language programming specification
- [CS231N]Lecture_ 2:Image Classification pipelin
- 表单验证和级联下拉列表(多种实现)
- Sword finger offer II 063. replacement word (medium prefix tree string)
- 小程序 监听目标节点出现到屏幕中
- Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom
- What is the difference between inline elements and block level elements? Semantic function
- Netease Yunxin 2022q2 product supply station, come and get your product supply plan!
- Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)
- Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)
猜你喜欢

什么是时间复杂度

Hcip experiment (14)

SQL injection less38 (Stack Injection)

SQL injection less42 (post stack injection)

Desai wisdom number - line chart (stacking area chart): ranking of deposits of different occupational groups in the proportion of monthly income in 2022

Embrace open source guidelines

罗克韦尔AB PLC RSLogix数字量IO模块基本介绍

JS DOM编程之平平无奇小练习

Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true

HCIP第七次实验
随机推荐
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
HCIP(11)
[leetcode] maximum depth of binary tree
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries
示波器发展史中的变化
CDN工作原理
MOV格式是不是静态图像文件格式
【云原生之kubernetes】在kubernetes集群下的映射外部服务—Eendpoint
Future trend of defi in bear market
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation
Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)
Ordinary practice of JS DOM programming
What does GPRS network mean
使用webWorker执行后台任务
[binary tree] pseudo palindrome path in binary tree
Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
Ruiji takeout project - development of business development function Day2
Learn kotlin - extension function
Alibaba cloud CDN practice
SQL注入 Less34(POST型宽字节注入+布尔盲注)