我正在研究leetcode problem。我想出了一个简单的解决方案,但它给出了错误的输出。
给出2D板和一个单词,查找单词是否存在于网格中。
单词可以由顺序相邻的字母构成 单元格,其中“相邻”单元格是水平或垂直的单元格 邻接。同一字母单元不得重复使用。
对于此输入:
面板:[[“ 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;
}
答案 0 :(得分: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;
}
答案 1 :(得分:0)
这是使用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();
}
}