#4 Daily Problems -Sudoku2

  • 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.
  • [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.
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;}

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Rook v1.1: Accelerating Storage Providers

60 Days of Flutter :Building a Messenger : Day 24–26 : Building a Animated Progress Fab and the…

Android JetPack Manager Components Part1:(ViewModel+LiveData + Paging Library)

Linear, a task management tool

Data structure alignment

CS373 Fall 2020 Week of 19 Oct.- 25 Oct.

News, Updates about Evolution

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arun Pandian M

Arun Pandian M

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

More from Medium

With Programmers: Week nth

版本控制(version control)實在太重要了 (上) 你會, 錄取!

Delete only one occurrence of 5 from a number and return the maximum number

Real Users, Real Clients at Encora Apprenticeship — Week 1