Implement the sudokuValidPlacementCount method that counts how many filled Sudoku cells are validly placed.

The input contains a Sudoku-style board and its size n.

Your task is to count how many filled positions are valid according to the placement rules. A filled value is valid when the same value does not appear again in its row, column, or related box area.

Empty cells are represented by . and are not treated as fixed duplicate values. The final count tells how many board positions pass the validity check.

Example 1

Input
board (char[][]) = [[5,3,.],[6,.,.],[.,9,8]]
n (int) = 3
Returns (int) 7

Example 2

Input
board (char[][]) = [[1,2,3],[4,5,6],[7,8,9]]
n (int) = 3
Returns (int) 9

Example 3

Input
board (char[][]) = [[1,1,3],[4,5,6],[7,8,9]]
n (int) = 3
Returns (int) 7

Check every filled cell against the rest of the board.

For each non-empty cell, compare its value with other cells in the same row and column. In a normal Sudoku board, you should also compare it with cells in the same sub-box. If another cell with the same value is found in any restricted area, that placement is not valid.

Count only the positions that pass the duplicate check.

Pseudocode:

function sudokuValidPlacementCount(board, n):
    count = 0
    for row from 0 to n - 1:
        for col from 0 to n - 1:
            if board[row][col] == '.':
                continue
            if isValidPlacement(row, col, board[row][col]):
                count++
    return count
function isValidPlacement(row, col, value):
    for each other column in the same row:
        if board[row][otherColumn] == value:
            return false
    for each other row in the same column:
        if board[otherRow][col] == value:
            return false
    check the related box area if required
    return true
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.