{"size": 1343, "ext": "js", "max_stars_repo_path": "LEETCODE/leetcode-javascript-master/Easy/36-validSudoku.js", "max_stars_repo_name": "bgoonz/DS-n-Algos-Mega-Archive", "max_stars_repo_stars_event_min_datetime": "2019-03-18T06:57:33.000Z", "max_stars_repo_stars_event_max_datetime": "2019-03-18T06:57:33.000Z", "max_issues_repo_path": "Easy/36-validSudoku.js", "max_issues_repo_name": "Ahmetburhan/LeetCode-JavaScript-AirBNB_style", "max_issues_repo_issues_event_min_datetime": "2021-05-02T10:59:18.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-14T10:02:22.000Z", "max_forks_repo_path": "Easy/36-validSudoku.js", "max_forks_repo_name": "Ahmetburhan/LeetCode-JavaScript-AirBNB_style", "max_forks_repo_forks_event_min_datetime": "2021-11-26T20:43:19.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-26T20:43:19.000Z"}
{"max_stars_count": 1, "max_issues_count": 25, "max_forks_count": 1, "avg_line_length": 33.575, "max_line_length": 98, "alphanum_fraction": 0.4408041698}
/**
 * use one row loop and one col loop to examine all three rules
 * row, column and sub-9-grid (the tricky part).
 *
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function(board) {
    if (!board || board.length !== 9 || board[0].length !== 9) return false;

    for (var i = 0; i < board.length; i++) {
        var row = {};
        var col = {};
        var grid = {};
        for (var j = 0; j < board.length; j++) {
            if (board[i][j] !== '.' && !(board[i][j] in row)) {
                row[board[i][j]] = true;
            } else if (board[i][j] !== '.' && (board[i][j] in row)) {
                return false;
            }

            if (board[j][i] !== '.' && !(board[j][i] in col)) {
                col[board[j][i]] = true;
            } else if (board[j][i] !== '.' && (board[j][i] in col)) {
                return false;
            }

            var rowIndex = 3 * Math.floor(i / 3) + Math.floor(j / 3);
            var colIndex = 3 * (i % 3) + (j % 3);
            if (board[rowIndex][colIndex] !== '.' && !(board[rowIndex][colIndex] in grid)) {
                grid[board[rowIndex][colIndex]] = true;
            } else if (board[rowIndex][colIndex] !== '.' && (board[rowIndex][colIndex] in grid)) {
                return false;
            }
        }
    }

    return true;
};
