C# Solution using Hashtable


  • 0
    H

    public class Solution {
    public bool IsValidSudoku(char[,] board) {

        Hashtable ht = new Hashtable();
        
        // Check rows
        for( int i = 0; i < 9; i++){
            ht.Clear();
            for(int j = 0; j < 9; j++){
                char c = board[i,j];
                if(c == '.') continue;
                if( ht.ContainsKey(c)) 
                    return false;
                ht.Add(c,1);
            }
        }
            
        // Check columns
        for( int i = 0; i < 9; i++){
            ht.Clear();
            for( int j = 0; j < 9; j++){
                char c = board[j,i];
                if(c == '.') continue;
                if( ht.ContainsKey(c)) 
                    return false;
                ht.Add(c,1);                
            }
        }
            
        //Check blocks
        for( int i = 0; i < 9; i++){
            ht.Clear();
            for(int j = 0; j < 9; j++){
                int i2 = (i/3)*3 +j/3;
                int j2 = (i%3)*3 +j%3;
                char c = board[i2,j2];
                if(c == '.') continue;
                if( ht.ContainsKey(c)) 
                    return false;
                ht.Add(c,1);  
            }
        }
        
        return true;
    }
    

    }


Log in to reply
 

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