Very easy Java Solution


  • 0
    K
     public class Solution {
        
        public void solve(char[][] board) {
    
    		if(board.length!=0){
    			int[][] t=new int[board.length][board[0].length];
    			for(int j=1 ; j<board[0].length-1; j++){
    				if(board[0][j]=='O'){
    					checkd(1,j,board,t);
    				}
    			}
    
    			for(int j=1 ; j<board[0].length-1; j++){
    				if(board[board.length-1][j]=='O'){
    					checku(board.length-2,j,board,t);
    				}
    			}
    
    			for(int i=1 ; i<board.length-1; i++){
    				if(board[i][0]=='O'){
    					checkr(i,1,board,t);
    				}
    			}
    
    			for(int i=1 ; i<board.length-1; i++){
    				if(board[i][board[0].length-1]=='O'){
    					checkl(i,board[0].length-2,board,t);
    				}
    			}
    
    			for(int i =1 ;i<board.length-1;i++ ){
    				for(int j=1 ; j<board[0].length-1; j++){
    					if(board[i][j]=='O' && t[i][j]!=1){
    						board[i][j]='X';
    					}
    				}
    			}
    		}
    	}
    
    	public void checkl(int i, int j, char[][] board, int[][] t){
    		if(j!=0 && board[i][j]=='O' && t[i][j]!=1){
    			t[i][j]=1;
    			checkl(i,j-1,board,t);
    			checkr(i,j+1,board,t);
    			checku(i-1,j,board,t);
    			checkd(i+1,j,board,t);
    		}
    	}
    
    	public void checku(int i, int j, char[][] board, int[][] t){
    		if(i!=0 && board[i][j]=='O' && t[i][j]!=1){
    			t[i][j]=1;
    			checkl(i,j-1,board,t);
    			checkr(i,j+1,board,t);
    			checku(i-1,j,board,t);
    			checkd(i+1,j,board,t);
    		}
    	}
    
    	public void checkr(int i, int j, char[][] board, int[][] t){
    		if(j!=board[i].length -1 && board[i][j]=='O' && t[i][j]!=1){
    			t[i][j]=1;
    			checkl(i,j-1,board,t);
    			checkr(i,j+1,board,t);
    			checku(i-1,j,board,t);
    			checkd(i+1,j,board,t);
    		}
    	}
    
    	public void checkd(int i, int j, char[][] board, int[][] t){
    		if(i!=board.length-1 && board[i][j]=='O' && t[i][j]!=1){
    			t[i][j]=1;
    			checkl(i,j-1,board,t);
    			checkr(i,j+1,board,t);
    			checku(i-1,j,board,t);
    			checkd(i+1,j,board,t);
    		}
    	}
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.