My simple JAVA solution using three iterations for all three cases


  • 1
    F
    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            
            for(int i=0;i<9;i++){
                int a[]=new int[9];
                for(int j=0;j<9;j++){
                    if(board[i][j]!='.'){
                        a[board[i][j]-'1']++;
                        if(a[board[i][j]-'1']>1){
                            return false;
                        }
                    }
                }
            }
            
            for(int i=0;i<9;i++){
                int a[]=new int[9];
                for(int j=0;j<9;j++){
                    if(board[j][i]!='.'){
                        a[board[j][i]-'1']++;
                        if(a[board[j][i]-'1']>1){
                            return false;
                        }
                    }
                }
            }
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    int a[]=new int[9];
                    for(int m=3*i;m<3+3*i;m++){
                    
                        for(int n=3*j;n<3+3*j;n++){
                            if(board[m][n]!='.'){
                                a[board[m][n]-'1']++;
                                if(a[board[m][n]-'1']>1){
                                    return false;
                                }
                            }          
                        }
                    }
                }
            }
            return true;
       }
    }

Log in to reply
 

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