Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@
- [Task #4](src/neetcode/arrays-&-hashing/group-anagrams) - _Group Anagrams_ - _[check the task](https://neetcode.io/problems/anagram-groups)_
- [Task #5](src/neetcode/arrays-&-hashing/top-k-frequent-elements) - _Top K Frequent Elements_ - _[check the task](https://neetcode.io/problems/top-k-elements-in-list)_
- [Task #6](src/neetcode/arrays-&-hashing/encode-and-decode-strings) - _Encode And Decode Strings_ - _[check the task](https://neetcode.io/problems/string-encode-and-decode)_
- [Task #7](src/neetcode/arrays-&-hashing/product-of-array-except-self) - _Products of Array Except Self_ - _[check the task](https://neetcode.io/problems/products-of-array-discluding-self)_
- [Task #7](src/neetcode/arrays-&-hashing/product-of-array-except-self) - _Products of Array Except Self_ - _[check the task](https://neetcode.io/problems/products-of-array-discluding-self)_
- [Task #8](src/neetcode/arrays-&-hashing/valid-sudoku) - _Valid Sudoku_ - _[check the task](https://neetcode.io/problems/valid-sudoku)_
63 changes: 63 additions & 0 deletions src/neetcode/arrays-&-hashing/valid-sudoku/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { isValidSudoku } from './solution';

describe('Valid Sudoku | NeetCode RoadMap | Testcases', () => {
test('#1 Valid', () => {
const board = [
['1', '2', '.', '.', '3', '.', '.', '.', '.'],
['4', '.', '.', '5', '.', '.', '.', '.', '.'],
['.', '9', '8', '.', '.', '.', '.', '.', '3'],
['5', '.', '.', '.', '6', '.', '.', '.', '4'],
['.', '.', '.', '8', '.', '3', '.', '.', '5'],
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
['.', '.', '.', '.', '.', '.', '2', '.', '.'],
['.', '.', '.', '4', '1', '9', '.', '.', '8'],
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
];
expect(isValidSudoku(board)).toBe(true);
});

test('#2 Invalid Line', () => {
const board = [
['.', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '9', '.', '.', '.', '.', '.', '.', '1'],
['8', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '9', '9', '3', '5', '7', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '4', '.'],
['.', '.', '.', '8', '.', '.', '.', '.', '.'],
['.', '1', '.', '.', '.', '.', '4', '.', '9'],
['.', '.', '.', '5', '.', '4', '.', '.', '.'],
];
expect(isValidSudoku(board)).toBe(false);
});

test('#3 Invalid Column', () => {
const board = [
['1', '2', '.', '.', '3', '.', '.', '.', '.'],
['4', '.', '.', '5', '.', '.', '.', '.', '.'],
['.', '9', '1', '.', '.', '.', '.', '.', '3'],
['5', '.', '.', '.', '6', '.', '.', '.', '4'],
['.', '.', '.', '8', '.', '3', '.', '.', '5'],
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
['.', '.', '.', '.', '.', '.', '2', '.', '.'],
['1', '.', '.', '4', '1', '9', '.', '.', '8'],
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
];
expect(isValidSudoku(board)).toBe(false);
});

test('#4 Invalid Square', () => {
const board = [
['1', '2', '.', '.', '3', '.', '.', '.', '.'],
['4', '.', '.', '5', '.', '.', '.', '.', '.'],
['.', '9', '1', '.', '.', '.', '.', '.', '3'],
['5', '.', '.', '.', '6', '.', '.', '.', '4'],
['.', '.', '.', '8', '.', '3', '.', '.', '5'],
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
['.', '.', '.', '.', '.', '.', '2', '.', '.'],
['.', '.', '.', '4', '1', '9', '.', '.', '8'],
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
];
expect(isValidSudoku(board)).toBe(false);
});
});
50 changes: 50 additions & 0 deletions src/neetcode/arrays-&-hashing/valid-sudoku/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @param {character[][]} board
* @return {boolean} `true` if the Sudoku board is valid, `false` otherwise
*/
export const isValidSudoku = (board: string[][]): boolean => {
for (const line of board) {
const numsCount = Array.from<number>({ length: board.length + 1 }).fill(0);
for (const el of line) {
const num = Number(el);
if (!isNaN(num)) {
numsCount[num]++;
if (numsCount[num] > 1) {
return false;
}
}
}
}

for (let i = 0; i < board.length; i++) {
const numsCount = Array.from<number>({ length: board.length + 1 }).fill(0);
for (let j = 0; j < board[i].length; j++) {
const num = Number(board[j][i]);
if (!isNaN(num)) {
numsCount[num]++;
if (numsCount[num] > 1) {
return false;
}
}
}
}

for (let i = 0; i < board.length; i += 3) {
for (let j = 0; j < board[i].length; j += 3) {
const numsCount = Array.from<number>({ length: board.length + 1 }).fill(0);
for (let k = 0; k < 3; k++) {
for (let l = 0; l < 3; l++) {
const num = Number(board[i + k][j + l]);
if (!isNaN(num)) {
numsCount[num]++;
if (numsCount[num] > 1) {
return false;
}
}
}
}
}
}

return true;
};
Loading