onCC
onCC

Reputation: 71

Having trouble with a Sudoku Solution Checker

I found an interesting coding challenge online. The task was to check a Sudoku program for its correctness. So this is the program that I wrote:

public static boolean check(int[][] sudoku) {
    String sudokuString = "";
    for(int[] a : sudoku)
      for(int b : a)
        sudokuString += b;
    for(int i = 0; i < 9; i++){
      for(int a = 1; a <= 9; a++){
        sudokuString = sudokuString.replaceFirst(Integer.toString(a), "");
      }
    }
    System.out.println(sudokuString);
    return sudokuString.length() == 0;
}

Now I'm pretty sure that my solution does what its supposed to, but the coding challenge website is telling me that its not. I don't understand why my solution isn't correct.

I'm asking for your help.

Thank you very much!

Upvotes: 0

Views: 71

Answers (1)

DwB
DwB

Reputation: 38338

Step 1 Understand the rules of Sudoku.
Sudoku is a 9x9 grid of digits. Each cell may contain a one digit integer value (1 - 9). Each column of the grid must contain each digit exactly one time. Each row of the grid must contain each digit exactly one time. Each 3x3 grid must contain each digit exactly one time.

Step 2 Pay attention to the rules.
The phrase "must contain each digit exactly one time" implies a Set.

Step 3 Devise an algorithm to solve the problem.
For each column, construct a set of digits. If (at the end of processing each column) the set contains nine elements, then the column complies with the rules of Sudoku.

For each row, construct a set of digits. If (at the end of processing each row) the set contains nine elements, then the row complies with the rules of Sudoku.

For each 3x3 grid, construct a set of digits. If (at the end of processing each 3x3 grid) the set contains nine elements, then the 3x3 grid complies with the rules of Sudoku.

Step 4 Write some code.
Have at.

Edit: Added 3x3 grid info.

Upvotes: 2

Related Questions