How to write a Java Program to validate whether a Sudoku is valid or not? | Technical, Finance, Investment Questions

How to write a Java Program to validate whether a Sudoku is valid or not?

Priya Singh

7 months ago

Introduction: Sudoku is a number game which is played on a 9x9 sudoku board.


Properties of Sudoku:


  1. The sudoku board is broken down into nine 3x3 squares.
  2. Sudoku board must contain the digits 1 through 9 only.
  3. Every Row must have the digits 1 through 9 only and all Unique Number.
  4. Every Column must have the digits 1 through 9 only and all Unique Number.


Problem: How to write a Java Program to validate whether a Sudoku is valid or not?


Solution: This Java Program will tell whether a Sudoku is valid or not.




Java Program:



package com.juliusbaer.toolbox.hiring.service;


import com.juliusbaer.toolbox.hiring.service.constants.Constants;


import java.util.ArrayList;

import java.util.HashSet;

import java.util.List;

import java.util.Set;


public class Sudoku {


  public int validate(int[][] board){

    return validateSudoku(board);

  }


  public int validate(int[][] board, int sizeOfSquire){

    try{

      int rowIndex = 0;

      int columnIndex = 0;

      for(int i=0;i<board.length;) {

        rowIndex = i;

        for (int j = 0; j < board.length;) {

          columnIndex = j;

          System.out.println("rowIndex : "+ rowIndex + " , columnIndex : "+ columnIndex);

          int[][] subSquireArray = getSubSquireArray(board, rowIndex, columnIndex, sizeOfSquire);

          int response = validateSudoku(subSquireArray);

          if(Constants.SUDOKU_VALIDATION_SUCCESS != response){

            return response;

          }

          j = j + 3;

        }

        i = i + sizeOfSquire;

      }

    }catch (Exception e){

      e.printStackTrace();

    }

    return Constants.SUDOKU_VALIDATION_SUCCESS;

  }


  private int[][] getSubSquireArray(int[][] board, int rowIndex, int columnIndex, int sizeOfSquire){

    int[][] outPut = new int[sizeOfSquire][sizeOfSquire];

    int row = 0;

    for(int i=rowIndex;i< rowIndex + sizeOfSquire;i++){

      int column = 0;

      for(int j=columnIndex;j<columnIndex+sizeOfSquire;j++){

        outPut[row][column] = board[i][j];

        column = column + 1;

      }

      row = row + 1;

    }

    //System.out.println("outPut : "+ outPut);

    return outPut;

  }


  private int validateSudoku(int[][] board) {

    List<Set<Integer>> rows = new ArrayList<>();

    for(int i=0;i<board.length;i++){

      Set<Integer> rowSet = new HashSet<>();

      for(int j=0;j<board.length;j++){

        int data = board[i][j];

        if(data >= 1 && data <= 9){

          boolean isDuplicate = rowSet.add(data);

          if(!isDuplicate){

            return Constants.SUDOKU_VALIDATION_DUPLICATE_FOUND;

          }

        }else{

          return Constants.SUDOKU_VALIDATION_NEGATIVE_FOUND;

        }

      }

      rows.add(rowSet);

    }


    List<Set<Integer>> columns = new ArrayList<>();

    for(int i=0;i<board.length;i++){

      Set<Integer> columnSet = new HashSet<>();

      for(int j=0;j<board.length;j++){

        int data = board[j][i];

        if(data >= 1 && data <= 9){

          boolean isDuplicate = columnSet.add(data);

          if(!isDuplicate){

            return Constants.SUDOKU_VALIDATION_DUPLICATE_FOUND;

          }

        }else{

          return Constants.SUDOKU_VALIDATION_NEGATIVE_FOUND;

        }

      }

      columns.add(columnSet);

    }


    if(rows.size() != columns.size()){

      return Constants.SUDOKU_VALIDATION_ROW_COLUMN_MISMATCH;

    }

    return Constants.SUDOKU_VALIDATION_SUCCESS;

  }


}



Priya Singh

Business Analyst at HCL Singapore

7 months ago

Featured Blogs
not found

Category: Investment

Author: Sushmita Pal

Posted : 14 hours ago

0( 0 Comments )
not found

Category: Investment

Author: Sushmita Pal

Posted : 3 days ago

0( 0 Comments )
not found

Category: Investment

Author: Sushmita Pal

Posted : 4 days ago

60( 0 Comments )
not found

Category: Startup

Author: Anushka Trivedi

Posted : 5 days ago

50( 0 Comments )
not found

Category: Investment

Author: Anushka Trivedi

Posted : 5 days ago

32( 0 Comments )
not found

Category: Investment

Author: Anushka Trivedi

Posted : 6 days ago

64( 0 Comments )
not found

Category: Startup

Author: Anushka Trivedi

Posted : 10 days ago

142( 0 Comments )
not found

Category: Startup

Author: Anushka Trivedi

Posted : 13 days ago

122( 0 Comments )
not found

Category: Technology

Author: Sweety Singh

Posted : 14 days ago

173( 0 Comments )
not found

Category: Stocks

Author: Sweety Singh

Posted : 16 days ago

69( 0 Comments )
Featured Questions
not found

Category: Technology

Author: Naveen Sharma

Posted : 6 months ago

276( 0 Comments )
not found

Category: Technology

Author: Priya Singh

Posted : 7 months ago

287( 0 Comments )

Category: Social

Author: Anish Srivastava

Posted : 11 months ago

18( 1 Comments )

Category: Social

Author: Anish Srivastava

Posted : 11 months ago

13( 1 Comments )

Category: Education

Author: Sachin Piramal

Posted : 1 years ago

10( 1 Comments )

Category: Property

Author: Ankit Kumar

Posted : 2 years ago

206( 0 Comments )

Category: Technology

Author: Ayush Raj

Posted : 2 years ago

48( 0 Comments )

Category: Technology

Author: WORA SETH

Posted : 2 years ago

23( 0 Comments )