mbhat2013
BAN USERVerified solution :
class Solution {
private static int[][] adj = {{0,1},{0,-1},{1,0},{-1,0}};
public boolean exist(char[][] board, String word) {
boolean result=false;
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
Set<String> path = new HashSet<>();
if(existRecursive(board,word,0,i,j,path)){
return true;
}
}
}
return result;
}
public boolean existRecursive(char[][] board, String word,int pos,int x, int y,Set<String> path) {
if(pos>=word.length()){
return true;
}
if(x>=0 && x<board.length
&& y>=0 && y<board[x].length
&& !path.contains(x+","+y) && word.charAt(pos)==board[x][y]){
path.add(x+","+y);
for(int j=0;j<adj.length;j++){
int x1=x+adj[j][0];
int y1=y+adj[j][1];
if(existRecursive(board,word,pos+1,x1,y1,path)){
return true;
}
}
path.remove(x+","+y);
}
return false;
}
}
Generic program with ability to set no of threads to print successive numbers
using wait and notify methods
- mbhat2013 April 13, 2019