Sudoku Valid Rows and Columns
JavaScript
Hard
3 views
Problem Description
Given 9x9 grid (0 means empty), check if rows and columns are valid (no duplicate 1..9). Print YES or NO.
Input Format
9 lines, each has 9 integers.
Sample Test Case
Input:
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
Constraints
All values are 0..9.
Official Solution
const fs=require('fs');const lines=fs.readFileSync(0,'utf8').trim().split(/\
?\
/);if(lines.length<9){process.stdout.write('NO');process.exit(0);}let g=[];for(let i=0;i<9;i++)g.push(lines[i].trim().split(/\\s+/).map(Number));let ok=true;for(let r=0;r<9&&ok;r++){let seen=new Uint8Array(10);for(let c=0;c<9;c++){const v=g[r][c];if(v===0)continue;if(v<1||v>9||seen[v]){ok=false;break;}seen[v]=1;}}for(let c=0;c<9&&ok;c++){let seen=new Uint8Array(10);for(let r=0;r<9;r++){const v=g[r][c];if(v===0)continue;if(v<1||v>9||seen[v]){ok=false;break;}seen[v]=1;}}process.stdout.write(ok?'YES':'NO');
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!