package leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * https://leetcode.com/problems/valid-sudoku/
 */
public class Problem36 {
    public boolean isValidSudoku(char[][] board) {
        boolean all = isValidSudokuFullGrid(board);
        if (!all) {
            return false;
        }
        for (int i = 0; i < board.length; i += 3) {
            for (int j = 0; j < board.length; j += 3) {
                if (!isValidSudokuSubGrid(board, i, i + 3, j, j + 3)) {
                    return false;
                }
            }
        }
        return true;
    }

    private boolean isValidSudokuFullGrid(char[][] board) {
        Map<Integer, Set<Character>> rowMap = new HashMap<>();
        Map<Integer, Set<Character>> colMap = new HashMap<>();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                char c = board[i][j];
                if (c == '.') {
                    continue;
                }
                if (!rowMap.containsKey(i)) {
                    rowMap.put(i, new HashSet<>());
                }
                if (!colMap.containsKey(j)) {
                    colMap.put(j, new HashSet<>());
                }
                Set<Character> rowSet = rowMap.get(i);
                if (rowSet.contains(c)) {
                    return false;
                } else {
                    rowSet.add(c);
                }
                Set<Character> colSet = colMap.get(j);
                if (colSet.contains(c)) {
                    return false;
                } else {
                    colSet.add(c);
                }
            }
        }
        return true;
    }

    private boolean isValidSudokuSubGrid(char[][] board, int startRow, int endRow,
                                         int startCol, int endCol) {
        Set<Character> set = new HashSet<>();
        for (int i = startRow; i < endRow; i++) {
            for (int j = startCol; j < endCol; j++) {
                char c = board[i][j];
                if (c == '.') {
                    continue;
                }
                if (set.contains(c)) {
                    return false;
                } else {
                    set.add(c);
                }
            }
        }
        return true;
    }
}

@@ -16,105 +16,8 @@
 e;%0A%0A
-import java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0A
 /**%0A
@@ -607,174 +607,144 @@
     
-Map%3CInteger, Set%3CCharacter%3E%3E rowMap = new HashMap%3C%3E();%0A        Map%3CInteger, Set%3CCharacter%3E%3E colMap = new HashMap%3C%3E();%0A        for (int i = 0; i %3C board.length; i++) %7B
+for (int row = 0; row %3C board.length; row++) %7B%0A            boolean%5B%5D rows = new boolean%5B9%5D;%0A            boolean%5B%5D cols = new boolean%5B9%5D;
 %0A   
@@ -757,32 +757,36 @@
    for (int 
-j
+col
  = 0; 
-j
+col
  %3C board%5Bi%5D.
@@ -782,17 +782,19 @@
 %3C board%5B
-i
+row
 %5D.length
@@ -795,17 +795,19 @@
 length; 
-j
+col
 ++) %7B%0A  
@@ -827,36 +827,40 @@
  char c = board%5B
-i%5D%5Bj
+row%5D%5Bcol
 %5D;%0A             
@@ -860,33 +860,33 @@
           if (c 
-=
+!
 = '.') %7B%0A       
@@ -886,52 +886,8 @@
     
-                continue;%0A                %7D%0A
     
@@ -906,85 +906,38 @@
 if (
-!
 row
-Map.containsKey(i)) %7B%0A                    rowMap.put(i, new HashSet%3C%3E());
+s%5Bboard%5Brow%5D%5Bcol%5D - '0'%5D) %7B
 %0A   
@@ -939,32 +939,24 @@
 %7B%0A          
-      %7D%0A
             
@@ -961,238 +961,97 @@
     
-  if (!colMap.containsKey(j)) %7B%0A                    colMap.put(j, new HashSet%3C%3E());%0A                %7D%0A                Set%3CCharacter%3E rowSet = rowMap.get(i);%0A                if (rowSet.contains(c)) %7B%0A                    return fals
+return false;%0A                    %7D%0A                    rows%5Bboard%5Brow%5D%5Bcol%5D - '0'%5D = tru
 e;%0A 
@@ -1058,39 +1058,33 @@
                %7D
- else %7B
+%0A
 %0A               
@@ -1088,142 +1088,120 @@
     
-    rowSet.add(c);%0A                %7D%0A                Set%3CCharacter%3E colSet = colMap.get(j);%0A                if (colSet.contains(c)) %7B%0A
+c = board%5Bcol%5D%5Brow%5D;%0A                if (c != '.') %7B%0A                    if (cols%5Bboard%5Bcol%5D%5Brow%5D - '0'%5D) %7B%0A    
     
@@ -1242,32 +1242,29 @@
             
-%7D else %7B
+    %7D
 %0A           
@@ -1279,18 +1279,39 @@
  col
-Set.add(c)
+s%5Bboard%5Bcol%5D%5Brow%5D - '0'%5D = true
 ;%0A  
@@ -1542,44 +1542,39 @@
     
-Set%3CCharacter%3E set = new HashSet%3C%3E()
+boolean%5B%5D nums = new boolean%5B9%5D
 ;%0A  
@@ -1817,23 +1817,31 @@
 if (
-set.contains(c)
+nums%5Bboard%5Bi%5D%5Bj%5D - '0'%5D
 ) %7B%0A
@@ -1895,15 +1895,8 @@
    %7D
- else %7B
 %0A   
@@ -1912,41 +1912,39 @@
     
-    set.add(c);%0A                %7D
+nums%5Bboard%5Bi%5D%5Bj%5D - '0'%5D = true;
 %0A   

Update problem 36