Valid Sudoku

Determine if a 9 x 9 The Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
Example 1:

Input: board = 
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true

Approach

Java


import java.util.HashSet;
import java.util.Set;

public class ValidSudoku {
    public static void main(String[] args) {
        char[][] board = { { '5''3''.''.''7''.''.''.''.' },
                { '6''.''.''1''9''5''.''.''.' }, 
                '.''9''8''.''.''.''.''6''.' },
                { '8''.''.''.''6''.''.''.''3' }, 
                '4''.''.''8''.''3''.''.''1' },
                { '7''.''.''.''2''.''.''.''6' },
                '.''6''.''.''.''.''2''8''.' },
                { '.''.''.''4''1''9''.''.''5' }, 
                '.''.''.''.''8''.''.''7''9' } };
        System.out.println(isValidSudoku(board));
    }

//function to check for the each row ,each column,each box of 3*3     
    static boolean isValid(char[][] boardint rowint col) {
        return Row(board, row) && Col(board, col) && Box(board, 
                    row - row % 3, col - col % 3);
    }

    static boolean isValidSudoku(char[][] board) {

//iterate for all cells
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {

                // if not valid then returnfalse
                if (!isValid(board, i, j)) {
                    return false;
                }
            }
        }
        return true;
    }

//function to check if row is valid or not
    static boolean Row(char[][] boardint row) {
        Set<Characterst = new HashSet<>();
        for (int i = 0; i < 9; i++) {
            if (st.contains(board[row][i]))
                return false;
            if (board[row][i] != '.')
                st.add(board[row][i]);
        }
        return true;
    }

//function to check if column is valid or not
    static boolean Col(char[][] boardint col) {
        Set<Characterst = new HashSet<Character>();
        for (int i = 0; i < 9; i++) {
            if (st.contains(board[i][col]))
                return false;
            if (board[i][col] != '.')
                st.add(board[i][col]);
        }
        return true;
    }

//function to check if box is valid
//or not
    static boolean Box(char[][] boardint rowint col) {
        Set<Characterst = new HashSet<Character>();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                char curr = board[i + row][j + col];
                if (st.contains(curr))
                    return false;
                else if (curr != '.')
                    st.add(curr);
            }
        }
        return true;
    }

}

C++

#include <bits/stdc++.h>
using namespace std;



//function to check if row is valid or not
bool Row(vector<vector<char>> &board,int row)
{
    set<charst;
    for(int i=0;i<9;i++)
        {
            if(st.find(board[row][i])!=st.end())
                  return false;
            if(board[row][i]!='.')
                   st.insert(board[row][i]);
       }
     return true;
}

//function to check if column is valid or not
bool Col(vector<vector<char>> &board,int col)
{
        set<charst;
        for(int i=0;i<9;i++)
        {
            if(st.find(board[i][col])!=st.end())
                  return false;
            if(board[i][col]!='.')
                   st.insert(board[i][col]);
        }
        return true;
}

//function to check if box is valid
//or not
bool Box(vector<vector<char>> &board,int row,int col)
{
    set<charst;
    for(int i=0;i<3;i++)
     {
        for(int j=0;j<3;j++)
            {
                char curr=board[i+row][j+col];
                if(st.find(curr)!=st.end())
                      return false;
                else if(curr!='.')
                       st.insert(curr);
            }
        }
        return true;
}
//function to check for the each row ,each column,each box of 3*3     
bool isValid(vector<vector<char>> &board,int row,int col)
{
    return Row(board,row)&&Col(board,col)&&Box(board,row-row%3,col-col%3);
}
bool isValidSudoku(vector<vector<char>>& board
{

    //iterate for all cells
    for(int i=0;i<9;i++)
      {
        for(int j=0;j<9;j++)
        {

            //if not valid then returnfalse
            if(!isValid(board,i,j))
                {
                     return false;
                }
        }
      }
  return true;
}

int main()
{
   vector<vector<char>> board
      {{'5','3','.','.','7','.','.','.','.'},
       {'6','.','.','1','9','5','.','.','.'},
       {'.','9','8','.','.','.','.','6','.'},
       {'8','.','.','.','6','.','.','.','3'},
       {'4','.','.','8','.','3','.','.','1'},
       {'7','.','.','.','2','.','.','.','6'},
       {'.','6','.','.','.','.','2','8','.'},
       {'.','.','.','4','1','9','.','.','5'},
       {'.','.','.','.','8','.','.','7','9'}};
   if(isValidSudoku(board))
      cout<<"true";
   else
    cout<<"false";
 
  return 0;
 
}


No comments:

Post a Comment