Monday, April 25, 2022

Leetcode discuss: 36. Valid Sudoku

 April 25, 2022

Here is the link. 

C# | Encode 1-9 digits to each row and each column and 9 3 x 3 matrix

April 19, 2022
Introduction
It takes me 10 minutes at least to write a working solution. First a hashset is created to include all digits from 1 to 9 to map for each row and each column and each of 9 3 x 3 matrix; next step is to go over each number in given matrix, and then remove it from the hashset.

The following C# code passes online judge.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _36_valid_sudoku___encode_as_strings
{
    class Program
    {
        static void Main(string[] args)
        {
            //[[".","8","7","6","5","4","3","2","1"],
            //["2",".",".",".",".",".",".",".","."],
            //["3",".",".",".",".",".",".",".","."],
            //["4",".",".",".",".",".",".",".","."],
            //["5",".",".",".",".",".",".",".","."],
            //["6",".",".",".",".",".",".",".","."],
            //["7",".",".",".",".",".",".",".","."],
            //["8",".",".",".",".",".",".",".","."],
            //["9",".",".",".",".",".",".",".","."]]
            var board = new char[9][];
            board[0] = new char[] { '.', '8', '7', '6', '5', '4', '3', '2', '1' };
            board[1] = new char[] { '2', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[2] = new char[] { '3', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[3] = new char[] { '4', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[4] = new char[] { '5', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[5] = new char[] { '6', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[6] = new char[] { '7', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[7] = new char[] { '8', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[8] = new char[] { '9', '.', '.', '.', '.', '.', '.', '.', '.' };

            var result = IsValidSudoku(board);
        }

        /// <summary>
        /// study code:
        /// https://leetcode.com/problems/valid-sudoku/discuss/15472/Short%2BSimple-Java-using-Strings
        /// Collect the set of things we see, encoded as strings. For example:
        /// '4' in row 7 is encoded as "(4)7".
        /// '4' in column 7 is encoded as "7(4)".
        /// '4' in the top-right block is encoded as "0(4)2".
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public static bool IsValidSudoku(char[][] board)
        {            
            var set = new HashSet<string>();
            var digits = "123456789".ToCharArray();            

            /*
             * Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
                Each row must contain the digits 1-9 without repetition.
                Each column must contain the digits 1-9 without repetition.
                Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
             */
            foreach(var digit in digits)
            {
                var digitStr = "(" + digit.ToString() + ")";
                // add all rows - ()0-9
                for (int index = 0; index < 9; ++index)
                {
                    set.Add(digitStr + index.ToString());  // every row at most 9 distinct digits
                    set.Add(index.ToString() + digitStr);  // every column 

                    set.Add(index % 3 + digitStr + index / 3);  // 9 3 x 3 matrixes 
                }
            }                          
            
            for (int row = 0; row < 9; ++row)
            {
                for (int col = 0; col < 9; ++col)
                {
                    if (board[row][col] == '.')
                    {
                        continue; 
                    }
                    
                    var b = "(" + board[row][col] + ")";

                    if (set.Contains(b + row) && set.Contains(col + b) && set.Contains(row / 3 + b + col / 3))
                    {
                        set.Remove(b + row);
                        set.Remove(col + b);
                        set.Remove(row / 3 + b + col / 3);
                    }
                    else 
                    {
                        return false; 
                    }
                    
                }
            }

            return true;
        }
    }
}

No comments:

Post a Comment