在字符网格中搜索单词
Posted
技术标签:
【中文标题】在字符网格中搜索单词【英文标题】:search a word in grid of characters 【发布时间】:2019-03-13 18:46:06 【问题描述】:我正在处理leetcode problem。我想出了以下简单的解决方案,但输出错误。
给定一个二维板和一个单词,找出该单词是否存在于网格中。
单词可以由顺序相邻的字母构成 单元格,其中“相邻”单元格是水平或垂直的单元格 邻接。同一个字母单元格不能多次使用。
对于这个输入:
棋盘:[["a","b"],["c","d"]]
单词:“abcd”
它应该返回 false,但下面的解决方案返回 true。
public static boolean exist(char[][] board, String word)
int row = board.length;
int col = board[0].length;
Map<Character, Integer> hm = new HashMap<Character, Integer>();
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
hm.put(board[i][j], hm.getOrDefault(board[i][j], 0) + 1);
char[] words = word.toCharArray();
for (int i = 0; i < words.length; i++)
char x = words[i];
if (hm.containsKey(x) && hm.get(x) > 0)
hm.put(x, hm.get(x) - 1);
else
return false;
return true;
【问题讨论】:
【参考方案1】:使用 DFS(深度优先搜索)算法:
public boolean exist(char[][] board, String word)
int m = board.length;
int n = board[0].length;
boolean result = false;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
if(dfs(board,word,i,j,0))
result = true;
return result;
public boolean dfs(char[][] board, String word, int i, int j, int k)
int m = board.length;
int n = board[0].length;
if(i<0 || j<0 || i>=m || j>=n)
return false;
if(board[i][j] == word.charAt(k))
char temp = board[i][j];
board[i][j]='#';
if(k==word.length()-1)
return true;
else if(dfs(board, word, i-1, j, k+1)
||dfs(board, word, i+1, j, k+1)
||dfs(board, word, i, j-1, k+1)
||dfs(board, word, i, j+1, k+1))
return true;
board[i][j]=temp;
return false;
【讨论】:
是的,这是一种方法。我在想我的方法有什么问题? 在您的解决方案中,您每次都在查看网格中的每个字母。【参考方案2】:这是一种不同的方法,它使用 Java 流。希望对您有所帮助!
import java.util.Arrays;
import java.util.stream.IntStream;
public class WordSearch
public static void main(String []args)
final char[][] matrix =
'X', 'H', 'A', 'T',
'X', 'E', 'X', 'X',
'X', 'L', 'X', 'O',
'X', 'L', 'X', 'W',
'C', 'O', 'O', 'L'
;
previewMatrix(matrix);
String[] searchSequences = "COOL", "HAT", "HELLO", "OWL", "WORLD";
for (String searchSequence : searchSequences)
boolean isWordFound = searchWord(searchSequence, matrix);
if (isWordFound)
System.out.printf("[ PASS ] The character sequence %s has been found in the matrix.\n", searchSequence);
else
System.out.printf("[ FAIL ] The character sequence %s was not found in the matrix.\n", searchSequence);
private static boolean searchWord(String searchSequence, char[][] matrix)
boolean isWordFound = false;
String rowString = null;
for (char[] row : matrix)
rowString = new String(row);
if (rowString.contains(searchSequence))
isWordFound = true;
break;
if (!isWordFound)
int columnsCount = matrix[0].length;
String colString = null;
for (int colIndex=0; colIndex < columnsCount; colIndex++)
colString = getColumnContent(matrix, colIndex);
if (colString.contains(searchSequence))
isWordFound = true;
break;
return isWordFound;
private static void previewMatrix(char[][] matrix)
for (char[] row : matrix)
for (char ch : row)
System.out.print(ch + " ");
System.out.println();
System.out.println();
private static String getColumnContent(char[][] matrix, int columnIndex)
return IntStream
.range(0, matrix.length)
.mapToObj(i -> (char) matrix[i][columnIndex])
.collect(
StringBuilder::new,
StringBuilder::appendCodePoint,
StringBuilder::append)
.toString();
【讨论】:
以上是关于在字符网格中搜索单词的主要内容,如果未能解决你的问题,请参考以下文章
2021-10-02:单词搜索。给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。单词必须按照字母