Determine if a 9 x 9 The Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9without repetition. - Each column must contain the digits
1-9without repetition. - Each of the nine
3 x 3sub-boxes of the grid must contain the digits1-9without 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: trueApproach
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*3static boolean isValid(char[][] board, int row, int col) {return Row(board, row) && Col(board, col) && Box(board,row - row % 3, col - col % 3);}static boolean isValidSudoku(char[][] board) {//iterate for all cellsfor (int i = 0; i < 9; i++) {for (int j = 0; j < 9; j++) {// if not valid then returnfalseif (!isValid(board, i, j)) {return false;}}}return true;}//function to check if row is valid or notstatic boolean Row(char[][] board, int row) {Set<Character> st = 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 notstatic boolean Col(char[][] board, int col) {Set<Character> st = 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 notstatic boolean Box(char[][] board, int row, int col) {Set<Character> st = 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 notbool Row(vector<vector<char>> &board,int row){set<char> st;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 notbool Col(vector<vector<char>> &board,int col){set<char> st;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 notbool Box(vector<vector<char>> &board,int row,int col){set<char> st;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*3bool 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 cellsfor(int i=0;i<9;i++){for(int j=0;j<9;j++){//if not valid then returnfalseif(!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";elsecout<<"false";return 0;}
No comments:
Post a Comment