-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWordSearch_Recursion.java
65 lines (54 loc) · 1.6 KB
/
WordSearch_Recursion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class WordSearch_Recursion {
/**
* DFS Technique used to solve
*/
String[][] input = new String[][] { { "A", "B", "C", "E" },
{ "S", "F", "C", "S" },
{ "A", "D", "E", "E" } };
int r = 0;
int c = 0;
public boolean searchWord(String matchingString){
r = input.length;
if(r == 0)
return false;
c = input[0].length;
if(c == 0)
return false;
boolean[][] visited = new boolean[r][c];
for(int i=0;i<r;i++){
for(int j = 0;j< c;j++){
if(findWord(input,i,j,0,matchingString,visited))
return true;
}
}
return false;
}
public boolean findWord(String[][] input,int row, int column, int match,
String matchingString, boolean[][] visited) {
if (match == matchingString.length()) {
return true;
}
//System.out.println(row+" "+column+" m "+match);
if(row < 0 || row >= r || column >= c || column<0){
return false;
}
if(visited[row][column])
return false;
String charAt = ""+matchingString.charAt(match);
if(!charAt.equals(input[row][column])){
return false;
}
visited[row][column] = true;
boolean result = (findWord(input,row+1,column,match+1,matchingString,visited)
|| findWord(input,row-1,column,match+1,matchingString,visited)
|| findWord(input,row,column+1,match+1,matchingString,visited)
|| findWord(input,row,column-1,match+1,matchingString,visited));
visited[row][column] = false;
return result;
}
public static void main(String[] args) {
WordSearch_Recursion wordSearch = new WordSearch_Recursion();
boolean findWord = wordSearch.searchWord("ABAB");
System.out.println("VALU "+findWord);
}
}