Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Sean Coughlin
Sean Coughlin

Posted on • Originally published atblog.seancoughlin.me on

     

How to validate a Sudoku board

The Problem

With this article, I will be covering thevalid sudoku Leetcode problem.

Leetcode describes the problem with the following:

Determine if a9 x 9 Sudoku board is valid. Only the filled cells need to be validatedaccording to the following rules :

  1. Each row must contain the digits1-9 without repetition.

  2. Each column must contain the digits1-9 without repetition.

  3. Each of the nine3 x 3 sub-boxes of the grid must contain the digits1-9 without repetition.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.

  • Only the filled cells need to be validated according to the mentioned rules.

Leetcode ranks this problem as a medium. I think that is an appropriate rating. The solution is feasible but does require some out-of-the-box reasoning.

The Solution

defisValidSudoku(self,board):seen=set()foriinrange(9):forjinrange(9):number=board[i][j]ifnumber!=".":row=str(number)+"row"+str(i)column=str(number)+"column"+str(j)block=str(number)+"block"+str(i/3)+"/"+str(j/3)ifrowinseenorcolumninseenorblockinseen:returnFalseseen.add(row)seen.add(column)seen.add(block)returnTrue
Enter fullscreen modeExit fullscreen mode

Solution Description

  1. seen = set(): Creates an empty set namedseen to keep track of numbers that have already appeared in rows, columns, and 3x3 blocks.

  2. Two nested loopsfor i in range(9) andfor j in range(9) iterate through each cell of the 9x9 board.

  3. number = board[i][j]: Stores the current cell's value in the variablenumber.

  4. if number != ".":: Checks if the cell is not empty (a dot indicates an empty cell in this context).

  5. row = str(number) + "row" + str(i): Creates a string like1row0 to indicate that the number 1 is in row 0.

  6. column = str(number) + "column" + str(j): Creates a string like1column0 to indicate that the number 1 is in column 0.

  7. block = str(number) + "block" + str(i / 3) + "/" + str(j/3): Creates a string like1block0/0 to indicate that the number 1 is in the top-left 3x3 block.

  8. if row in seen or column in seen or block in seen:: Checks if any of these strings already exist in theseen set. If so, the board is not valid and the function returnsFalse.

  9. seen.add(row),seen.add(column),seen.add(block): Adds the newly created strings to theseen set so that they can be checked against future cells.

  10. If all cells are valid, the function returnsTrue, indicating a valid Sudoku board.

Big O Analysis

Assumingn is the side length. We have a double for-loop so that is at leastO(n^2). Inside the nested loops, the operations (like adding to a set, creating strings, and checking for membership in a set) areO(1) operations. Therefore, the overall runtime isO(n^2).


Originally published athttps://blog.seancoughlin.me.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer
  • Location
    Chicago, IL
  • Education
    University of Illinois Urbana-Champaign
  • Joined

More fromSean Coughlin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp