c# O(n^2) solution,


  • 0
    N
    public class Solution {
        public bool IsValidSudoku(char[,] board) {
            for(var i=0;i<9;++i){
                var col = new HashSet<char>();
                var row = new HashSet<char>();
                var sub = new HashSet<char>();
    
                for(var j=0;j<9;j++){
                    
                    if(board[i,j]!='.' && col.Contains(board[i,j])) return false;
                    col.Add(board[i,j]);
                    
                    if(board[j,i]!='.' && row.Contains(board[j,i])) return false;
                    row.Add(board[j,i]);
                    
                    var x = (i%3)*3 + j%3;
                    var y = (i/3)*3 + j/3;
                    if(board[x,y]!='.' && sub.Contains(board[x,y])) return false;
                    sub.Add(board[x,y]);
                }
            }
            
            return true;
        }
    }
    

Log in to reply
 

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