#4 Daily Problems -Sudoku2

Arun Pandian M
3 min readJul 21, 2020

--

Hi Friends, I have solved the array related problem in fourth day.

Problem:

Sudoku is a number-placement puzzle. The objective is to fill a 9 × 9 grid with numbers in such a way that each column, each row, and each of the nine 3 × 3 sub-grids that compose the grid all contain all of the numbers from 1 to 9 one time.

Implement an algorithm that will check whether the given grid of numbers represents a valid Sudoku puzzle according to the layout rules described above. Note that the puzzle represented by grid does not have to be solvable.

Example

  • For
  • grid = [['.', '.', '.', '1', '4', '.', '.', '2', '.'], ['.', '.', '6', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '1', '.', '.', '.', '.', '.', '.'], ['.', '6', '7', '.', '.', '.', '.', '.', '9'], ['.', '.', '.', '.', '.', '.', '8', '1', '.'], ['.', '3', '.', '.', '.', '.', '.', '.', '6'], ['.', '.', '.', '.', '.', '7', '.', '.', '.'], ['.', '.', '.', '5', '.', '.', '.', '7', '.']
  • the output should be
    sudoku2(grid) = true;
  • For
  • grid = [['.', '.', '.', '.', '2', '.', '.', '9', '.'], ['.', '.', '.', '.', '6', '.', '.', '.', '.'], ['7', '1', '.', '.', '7', '5', '.', '.', '.'], ['.', '7', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '8', '3', '.', '.', '.'], ['.', '.', '8', '.', '.', '7', '.', '6', '.'], ['.', '.', '.', '.', '.', '2', '.', '.', '.'], ['.', '1', '.', '2', '.', '.', '.', '.', '.'], ['.', '2', '.', '.', '3', '.', '.', '.', '.']]
  • the output should be
    sudoku2(grid) = false.
  • The given grid is not correct because there are two 1s in the second column. Each column, each row, and each 3 × 3 subgrid can only contain the numbers 1 through 9 one time.

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] array.array.char grid
  • A 9 × 9 array of characters, in which each character is either a digit from '1' to '9' or a period '.'.
  • [output] boolean
  • Return true if grid represents a valid Sudoku puzzle, otherwise return false.

My solution:

boolean sudoku2(char[][] grid) {int rowLength = grid.length;int colLenght = grid.length;System.out.println("rowLenght : "+rowLength+ "   colLenght: "+colLenght);for (int r = 0; r < rowLength; r++) {HashSet<Character> hashCol = new HashSet<Character>();for (int c = 0; c < colLenght; c++) {HashSet<Character> hashRow = new HashSet<Character>();for (int row=0;row<rowLength;row++){if (grid[row][c] != '.') {//                    System.out.println("value exist: "+hashRowCol.contains(grid[r][c]));if (hashRow.contains(grid[row][c]))return false;elsehashRow.add(grid[row][c]);}}if (grid[r][c] != '.') {//                    System.out.println("value exist: "+hashRowCol.contains(grid[r][c]));if (hashCol.contains(grid[r][c]))return false;elsehashCol.add(grid[r][c]);}if ((r==0 || r==3 | r==6 )&& (c==0 || c==3 || c==6)) {HashSet<Character> hashSub = new HashSet<Character>();for (int sr = r; sr < r + 3; sr++) { for (int sc = c; sc < c + 3; sc++) {if (grid[sr][sc] != '.') {if (hashSub.contains(grid[sr][sc]))return false;elsehashSub.add(grid[sr][sc]);}}}}}}return true;}

I guess I have used some loops to complete this task. I have to work on that thing try to optimize this solution. Please give me any suggestion to improve this solution. thanks guys

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Arun Pandian M
Arun Pandian M

Written by Arun Pandian M

Senior Android developer at FundsIndia, A time investor to learn new things about Programming. Currently in a relationship with Green Bug(Android).

Responses (2)

Write a response