Intermediate Level

Implement the validSudokuRowsSimple method that checks whether each row of a Sudoku board is valid.

The input contains a character matrix board and its size n. Your task is to check whether each row is valid.

For this simplified Sudoku-style problem, a row is valid when it does not contain the same non-empty value more than once in that row.

For example, the row [1,2,3] is valid, but [1,1,3] is invalid because 1 appears twice in the same row.

Example 1

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

Example 2

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

Example 3

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

Check each row separately using a set.

For every row, clear the set before scanning its cells. If a value already exists in the set, that row contains a duplicate and the board fails the row validation.

If all rows are scanned without finding a duplicate, return true.

Pseudocode:

function validSudokuRowsSimple(board, n):
    for r from 0 to n - 1:
        create empty set seen
        for c from 0 to n - 1:
            value = board[r][c]
            if value exists in seen:
                return false
            add value to seen
    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.