In: Computer Science
Code using JAVA:
must include a "Main Method" to run on "intelliJ".
Hint: Use a hash table. You can use either Java HashTable or Java HashMap. Refer to Java API for the subtle differences between HashTable and HashMap.
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
class Solution {
public boolean isValidSudoku(char[][] board) {
}
}
Note:
Example 1:
Input: board = [["5","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]] Output: true
Example 2:
Input: board = [["8","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]] Output: false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Constraints:
Below is the Java code:
import java.util.*;
class Solution {
public boolean isValidSudoku(char[][] board) {
HashMap<Integer,ArrayList<Character>> rmap=new HashMap<Integer,ArrayList<Character>>();
HashMap<Integer,ArrayList<Character>> cmap=new HashMap<Integer,ArrayList<Character>>();
for (int i = 0; i < 9; i++) { // Put all keys in map
rmap.put (i, new ArrayList<Character>());
cmap.put (i, new ArrayList<Character>());
}
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') continue;
Character val = board[i][j];
if (rmap.get(i).contains (val)) // Row already contains this value
return false;
else
rmap.get (i).add (val);
if (cmap.get(j).contains (val)) // Column already contains this value
return false;
else
cmap.get (j).add (val);
}
// Now check for the 3 x 3 matrices and confirm if there are no duplicates
for (int k = 0; k < 9; k++) {
// Starting elements of 3 x 3 matrices are 00, 03, 06, 30, 33, 36, 60, 63, 66
int i = 3 * (k / 3);
int j = 3 * (k % 3);
char [] temp = {board [i][j], board [i][j+1], board [i][j+2],
board [i+1][j], board [i+1][j+1], board [i+1][j+2],
board [i+2][j], board [i+2][j+1], board [i+2][j+2] };
if (!isValidSetOf9 (temp))
return false;
}
return true;
}
private boolean isValidSetOf9 (char[] set9) {
// Use similar technique as isValidSudoku but apply to one dimensional array of 9 elements
HashMap<Integer,ArrayList<Character>> map=new HashMap<Integer,ArrayList<Character>>();
map.put (1, new ArrayList<Character>());
for (int i = 0; i < 9; i++) {
if (set9[i] == '.') continue;
Character val = set9[i];
if (map.get(1).contains (val))
return false;
else
map.get (1).add (val);
}
return true;
}
}
A main method to test:
public class Main
{
public static void main(String[] args) {
char [][] matrix =
{{'5','3','.','.','7','.','.','.','.'}
,{'6','.','.','1','9','5','.','.','.'}
,{'.','9','8','.','.','.','.','6','.'}
,{'8','.','.','.','6','.','.','.','3'}
,{'4','.','.','8','.','3','.','.','1'}
,{'7','.','.','.','2','.','.','.','6'}
,{'.','6','.','.','.','.','2','8','.'}
,{'.','.','.','4','1','9','.','.','5'}
,{'.','.','.','.','8','.','.','7','9'}};
Solution s = new Solution ();
if (s.isValidSudoku (matrix))
System.out.println ("Yes");
else
System.out.println ("No");
}
}