36. Valid sudoku

36_solution1_bruteforce.c

#include <stdbool.h>
#include <string.h>

bool isValidSudoku(char **board, int boardSize, int *boardColSize) {
  bool seen[9] = {false};

  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      if (board[i][j] == '.')
        continue;
      int current = board[i][j] - '0' - 1;
      if (seen[current])
        return false;
      seen[current] = true;
    }
    memset(seen, false, sizeof(seen));
  }

  for (int j = 0; j < 9; j++) {
    for (int i = 0; i < 9; i++) {
      if (board[i][j] == '.')
        continue;
      int current = board[i][j] - '0' - 1;
      if (seen[current])
        return false;
      seen[current] = true;
    }
    memset(seen, false, sizeof(seen));
  }

  for (int rowOffset = 0; rowOffset < 9; rowOffset += 3) {
    for (int colOffset = 0; colOffset < 9; colOffset += 3) {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          if (board[i + rowOffset][j + colOffset] == '.')
            continue;
          int current = board[i + rowOffset][j + colOffset] - '0' - 1;
          if (seen[current])
            return false;
          seen[current] = true;
        }
      }
      memset(seen, false, sizeof(seen));
    }
  }

  return true;
}

36_solution2_onepass_offsets.c

#include <stdbool.h>
#include <string.h>

bool isValidSudoku(char **board, int boardSize, int *boardColSize) {
  bool seenGrid[9] = {false};
  bool seenCol[9][9] = {{false}};
  bool seenRow[9][9] = {{false}};

  for (int rowOffset = 0; rowOffset < 9; rowOffset += 3) {
    for (int colOffset = 0; colOffset < 9; colOffset += 3) {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          if (board[i + rowOffset][j + colOffset] == '.')
            continue;

          int current = board[i + rowOffset][j + colOffset] - '0' - 1;

          if (seenGrid[current])
            return false;
          seenGrid[current] = true;

          if (seenRow[i + rowOffset][current])
            return false;
          seenRow[i + rowOffset][current] = true;

          if (seenCol[j + colOffset][current])
            return false;
          seenCol[j + colOffset][current] = true;
        }
      }
      memset(seenGrid, false, sizeof(seenGrid));
    }
  }

  return true;
}

36_solution3_onepass_formula.c

#include <stdbool.h>

static bool checkAndMark(bool arr[9], int idx) {
  if (arr[idx])
    return false;
  arr[idx] = true;
  return true;
}

bool isValidSudoku(char **board, int boardSize, int *boardColSize) {
  bool seenRows[9][9] = {{false}};
  bool seenCols[9][9] = {{false}};
  bool seenBoxes[9][9] = {{false}};

  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      if (board[i][j] == '.')
        continue;

      int current = board[i][j] - '0' - 1;
      int box = (i / 3) * 3 + (j / 3);

      if (!checkAndMark(seenRows[i], current))
        return false;
      if (!checkAndMark(seenCols[j], current))
        return false;
      if (!checkAndMark(seenBoxes[box], current))
        return false;
    }
  }

  return true;
}

36_solution4_bitmasking.c

#include <stdbool.h>

static bool checkAndMark(int *mask, int idx) {
  if ((*mask & (1 << idx)) != 0)
    return false;
  *mask |= (1 << idx);
  return true;
}

bool isValidSudoku(char **board, int boardSize, int *boardColSize) {
  int seenRows[9] = {0};
  int seenCols[9] = {0};
  int seenBoxes[9] = {0};

  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      if (board[i][j] == '.')
        continue;

      int current = board[i][j] - '0' - 1;
      int box = (i / 3) * 3 + (j / 3);

      if (!checkAndMark(&seenRows[i], current))
        return false;
      if (!checkAndMark(&seenCols[j], current))
        return false;
      if (!checkAndMark(&seenBoxes[box], current))
        return false;
    }
  }

  return true;
}

36_solution1_bruteforce.cpp

#include <vector>
using namespace std;

class Solution {
public:
  bool isValidSudoku(vector<vector<char>> &board) {
    bool seen[9] = {};

    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        if (board[i][j] == '.')
          continue;
        int current = board[i][j] - '0' - 1;
        if (seen[current]) {
          return false;
        } else {
          seen[current] = true;
        }
      } // cols
      fill(begin(seen), end(seen), false);
    } // rows

    for (int j = 0; j < 9; j++) {
      for (int i = 0; i < 9; i++) {
        if (board[i][j] == '.')
          continue;
        int current = board[i][j] - '0' - 1;
        if (seen[current]) {
          return false;
        } else {
          seen[current] = true;
        }
      } // rows
      fill(begin(seen), end(seen), false);
    } // cols

    for (int rowOffset = 0; rowOffset < 9; rowOffset += 3) {
      for (int colOffset = 0; colOffset < 9; colOffset += 3) {

        for (int i = 0; i < 3; i++) {
          for (int j = 0; j < 3; j++) {
            if (board[i + rowOffset][j + colOffset] == '.')
              continue;
            int current = board[i + rowOffset][j + colOffset] - '0' - 1;
            if (seen[current]) {
              return false;
            } else {
              seen[current] = true;
            }
          }
        } // grid 3x3
        fill(begin(seen), end(seen), false);
      }
    } // all grids

    return true;
  }
};

36_solution2_onepass_offsets.cpp

#include <vector>
using namespace std;

class Solution {
public:
  bool isValidSudoku(vector<vector<char>> &board) {
    bool seenGrid[9] = {};
    bool seenCol[9][9] = {{}};
    bool seenRow[9][9] = {{}};

    for (int rowOffset = 0; rowOffset < 9; rowOffset += 3) {
      for (int colOffset = 0; colOffset < 9; colOffset += 3) {

        for (int i = 0; i < 3; i++) {
          for (int j = 0; j < 3; j++) {
            if (board[i + rowOffset][j + colOffset] == '.')
              continue;

            int current = board[i + rowOffset][j + colOffset] - '0' - 1;

            // current grid
            if (seenGrid[current]) {
              return false;
            } else {
              seenGrid[current] = true;
            }

            // row
            if (seenRow[i + rowOffset][current]) {
              return false;
            } else {
              seenRow[i + rowOffset][current] = true;
            }

            // cols
            if (seenCol[j + colOffset][current]) {
              return false;
            } else {
              seenCol[j + colOffset][current] = true;
            }
          }
        } // grid 3x3
        fill(begin(seenGrid), end(seenGrid), false);
      }
    }

    return true;
  }
};

36_solution3_onepass_formula.cpp

#include <vector>
using namespace std;

class Solution {
public:
  bool isValidSudoku(vector<vector<char>> &board) {
    bool seenRows[9][9] = {};
    bool seenCols[9][9] = {};
    bool seenBoxes[9][9] = {};

    auto checkAndMark = [](bool arr[9], int idx) {
      if (arr[idx])
        return false;
      arr[idx] = true;
      return true;
    };

    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        if (board[i][j] == '.')
          continue;

        int current = board[i][j] - '0' - 1;
        int box = (i / 3) * 3 + (j / 3);

        if (!checkAndMark(seenRows[i], current))
          return false;
        if (!checkAndMark(seenCols[j], current))
          return false;
        if (!checkAndMark(seenBoxes[box], current))
          return false;
      }
    }

    return true;
  }
};

36_solution4_bitmasking.cpp

#include <vector>
using namespace std;

class Solution {
public:
  bool isValidSudoku(vector<vector<char>> &board) {
    int seenRows[9] = {0};
    int seenCols[9] = {0};
    int seenBoxes[9] = {0};

    auto checkAndMark = [](int &mask, int idx) {
      if ((mask & (1 << idx)) != 0)
        return false;
      mask = mask | (1 << idx);
      return true;
    };

    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        if (board[i][j] == '.')
          continue;

        int current = board[i][j] - '0' - 1;
        int box = (i / 3) * 3 + (j / 3);

        if (!checkAndMark(seenRows[i], current))
          return false;
        if (!checkAndMark(seenCols[j], current))
          return false;
        if (!checkAndMark(seenBoxes[box], current))
          return false;
      }
    }

    return true;
  }
};

36_solution1_bruteforce.go

package main

func isValidSudoku(board [][]byte) bool {
    var seen [9]bool

    for i := 0; i < 9; i++ {
        for j := 0; j < 9; j++ {
            if board[i][j] == '.' {
                continue
            }
            current := board[i][j] - '0' - 1
            if seen[current] {
                return false
            }
            seen[current] = true
        }
        seen = [9]bool{}
    }

    for j := 0; j < 9; j++ {
        for i := 0; i < 9; i++ {
            if board[i][j] == '.' {
                continue
            }
            current := board[i][j] - '0' - 1
            if seen[current] {
                return false
            }
            seen[current] = true
        }
        seen = [9]bool{}
    }

    for rowOffset := 0; rowOffset < 9; rowOffset += 3 {
        for colOffset := 0; colOffset < 9; colOffset += 3 {
            for i := 0; i < 3; i++ {
                for j := 0; j < 3; j++ {
                    val := board[i+rowOffset][j+colOffset]
                    if val == '.' {
                        continue
                    }
                    current := val - '0' - 1
                    if seen[current] {
                        return false
                    }
                    seen[current] = true
                }
            }
            seen = [9]bool{}
        }
    }

    return true
}

36_solution2_onepass_offsets.go

package main

func isValidSudoku(board [][]byte) bool {
    var seenGrid [9]bool
    var seenCol [9][9]bool
    var seenRow [9][9]bool

    for rowOffset := 0; rowOffset < 9; rowOffset += 3 {
        for colOffset := 0; colOffset < 9; colOffset += 3 {
            for i := 0; i < 3; i++ {
                for j := 0; j < 3; j++ {
                    val := board[i+rowOffset][j+colOffset]
                    if val == '.' {
                        continue
                    }

                    current := val - '0' - 1

                    if seenGrid[current] {
                        return false
                    }
                    seenGrid[current] = true

                    if seenRow[i+rowOffset][current] {
                        return false
                    }
                    seenRow[i+rowOffset][current] = true

                    if seenCol[j+colOffset][current] {
                        return false
                    }
                    seenCol[j+colOffset][current] = true
                }
            }
            seenGrid = [9]bool{}
        }
    }

    return true
}

36_solution3_onepass_formula.go

package main

func checkAndMark(arr *[9]bool, idx byte) bool {
    if arr[idx] {
        return false
    }
    arr[idx] = true
    return true
}

func isValidSudoku(board [][]byte) bool {
    var seenRows [9][9]bool
    var seenCols [9][9]bool
    var seenBoxes [9][9]bool

    for i := 0; i < 9; i++ {
        for j := 0; j < 9; j++ {
            if board[i][j] == '.' {
                continue
            }

            current := board[i][j] - '0' - 1
            box := (i/3)*3 + (j / 3)

            if !checkAndMark(&seenRows[i], current) {
                return false
            }
            if !checkAndMark(&seenCols[j], current) {
                return false
            }
            if !checkAndMark(&seenBoxes[box], current) {
                return false
            }
        }
    }

    return true
}

36_solution4_bitmasking.go

package main

func checkAndMarkBit(masks *[9]int, idx int, bit byte) bool {
    if masks[idx]&(1<<bit) != 0 {
        return false
    }
    masks[idx] |= 1 << bit
    return true
}

func isValidSudoku(board [][]byte) bool {
    var seenRows [9]int
    var seenCols [9]int
    var seenBoxes [9]int

    for i := 0; i < 9; i++ {
        for j := 0; j < 9; j++ {
            if board[i][j] == '.' {
                continue
            }

            current := board[i][j] - '0' - 1
            box := (i/3)*3 + (j / 3)

            if !checkAndMarkBit(&seenRows, i, current) {
                return false
            }
            if !checkAndMarkBit(&seenCols, j, current) {
                return false
            }
            if !checkAndMarkBit(&seenBoxes, box, current) {
                return false
            }
        }
    }

    return true
}

36_solution1_bruteforce.java

import java.util.Arrays;

class Solution {
    public boolean isValidSudoku(char[][] board) {
        boolean[] seen = new boolean[9];

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] == '.')
                    continue;
                int current = board[i][j] - '0' - 1;
                if (seen[current])
                    return false;
                seen[current] = true;
            }
            Arrays.fill(seen, false);
        }

        for (int j = 0; j < 9; j++) {
            for (int i = 0; i < 9; i++) {
                if (board[i][j] == '.')
                    continue;
                int current = board[i][j] - '0' - 1;
                if (seen[current])
                    return false;
                seen[current] = true;
            }
            Arrays.fill(seen, false);
        }

        for (int rowOffset = 0; rowOffset < 9; rowOffset += 3) {
            for (int colOffset = 0; colOffset < 9; colOffset += 3) {
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        if (board[i + rowOffset][j + colOffset] == '.')
                            continue;
                        int current = board[i + rowOffset][j + colOffset] - '0' - 1;
                        if (seen[current])
                            return false;
                        seen[current] = true;
                    }
                }
                Arrays.fill(seen, false);
            }
        }

        return true;
    }
}

36_solution2_onepass_offsets.java

import java.util.Arrays;

class Solution {
    public boolean isValidSudoku(char[][] board) {
        boolean[] seenGrid = new boolean[9];
        boolean[][] seenCol = new boolean[9][9];
        boolean[][] seenRow = new boolean[9][9];

        for (int rowOffset = 0; rowOffset < 9; rowOffset += 3) {
            for (int colOffset = 0; colOffset < 9; colOffset += 3) {
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        if (board[i + rowOffset][j + colOffset] == '.')
                            continue;

                        int current = board[i + rowOffset][j + colOffset] - '0' - 1;

                        if (seenGrid[current])
                            return false;
                        seenGrid[current] = true;

                        if (seenRow[i + rowOffset][current])
                            return false;
                        seenRow[i + rowOffset][current] = true;

                        if (seenCol[j + colOffset][current])
                            return false;
                        seenCol[j + colOffset][current] = true;
                    }
                }
                Arrays.fill(seenGrid, false);
            }
        }

        return true;
    }
}

36_solution3_onepass_formula.java

class Solution {
    public boolean isValidSudoku(char[][] board) {
        boolean[][] seenRows = new boolean[9][9];
        boolean[][] seenCols = new boolean[9][9];
        boolean[][] seenBoxes = new boolean[9][9];

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] == '.')
                    continue;

                int current = board[i][j] - '0' - 1;
                int box = (i / 3) * 3 + (j / 3);

                if (!checkAndMark(seenRows[i], current))
                    return false;
                if (!checkAndMark(seenCols[j], current))
                    return false;
                if (!checkAndMark(seenBoxes[box], current))
                    return false;
            }
        }

        return true;
    }

    private boolean checkAndMark(boolean[] arr, int idx) {
        if (arr[idx])
            return false;
        arr[idx] = true;
        return true;
    }
}

36_solution4_bitmasking.java

class Solution {
    public boolean isValidSudoku(char[][] board) {
        int[] seenRows = new int[9];
        int[] seenCols = new int[9];
        int[] seenBoxes = new int[9];

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] == '.')
                    continue;

                int current = board[i][j] - '0' - 1;
                int box = (i / 3) * 3 + (j / 3);

                if (!checkAndMark(seenRows, i, current))
                    return false;
                if (!checkAndMark(seenCols, j, current))
                    return false;
                if (!checkAndMark(seenBoxes, box, current))
                    return false;
            }
        }

        return true;
    }

    private boolean checkAndMark(int[] masks, int idx, int bit) {
        if ((masks[idx] & (1 << bit)) != 0)
            return false;
        masks[idx] |= (1 << bit);
        return true;
    }
}

36_solution1_bruteforce.js

/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function (board) {
  let seen = new Array(9).fill(false);

  for (let i = 0; i < 9; i++) {
    for (let j = 0; j < 9; j++) {
      if (board[i][j] === ".") continue;
      const current = board[i][j].charCodeAt(0) - "0".charCodeAt(0) - 1;
      if (seen[current]) return false;
      seen[current] = true;
    }
    seen.fill(false);
  }

  for (let j = 0; j < 9; j++) {
    for (let i = 0; i < 9; i++) {
      if (board[i][j] === ".") continue;
      const current = board[i][j].charCodeAt(0) - "0".charCodeAt(0) - 1;
      if (seen[current]) return false;
      seen[current] = true;
    }
    seen.fill(false);
  }

  for (let rowOffset = 0; rowOffset < 9; rowOffset += 3) {
    for (let colOffset = 0; colOffset < 9; colOffset += 3) {
      for (let i = 0; i < 3; i++) {
        for (let j = 0; j < 3; j++) {
          if (board[i + rowOffset][j + colOffset] === ".") continue;
          const current =
            board[i + rowOffset][j + colOffset].charCodeAt(0) -
            "0".charCodeAt(0) -
            1;
          if (seen[current]) return false;
          seen[current] = true;
        }
      }
      seen.fill(false);
    }
  }

  return true;
};

36_solution2_onepass_offsets.js

/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function (board) {
  let seenGrid = new Array(9).fill(false);
  const seenCol = Array.from({ length: 9 }, () => new Array(9).fill(false));
  const seenRow = Array.from({ length: 9 }, () => new Array(9).fill(false));

  for (let rowOffset = 0; rowOffset < 9; rowOffset += 3) {
    for (let colOffset = 0; colOffset < 9; colOffset += 3) {
      for (let i = 0; i < 3; i++) {
        for (let j = 0; j < 3; j++) {
          if (board[i + rowOffset][j + colOffset] === ".") continue;

          const current =
            board[i + rowOffset][j + colOffset].charCodeAt(0) -
            "0".charCodeAt(0) -
            1;

          if (seenGrid[current]) return false;
          seenGrid[current] = true;

          if (seenRow[i + rowOffset][current]) return false;
          seenRow[i + rowOffset][current] = true;

          if (seenCol[j + colOffset][current]) return false;
          seenCol[j + colOffset][current] = true;
        }
      }
      seenGrid.fill(false);
    }
  }

  return true;
};

36_solution3_onepass_formula.js

/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function (board) {
  const seenRows = Array.from({ length: 9 }, () => new Array(9).fill(false));
  const seenCols = Array.from({ length: 9 }, () => new Array(9).fill(false));
  const seenBoxes = Array.from({ length: 9 }, () => new Array(9).fill(false));

  const checkAndMark = (arr, idx) => {
    if (arr[idx]) return false;
    arr[idx] = true;
    return true;
  };

  for (let i = 0; i < 9; i++) {
    for (let j = 0; j < 9; j++) {
      if (board[i][j] === ".") continue;

      const current = board[i][j].charCodeAt(0) - "0".charCodeAt(0) - 1;
      const box = Math.floor(i / 3) * 3 + Math.floor(j / 3);

      if (!checkAndMark(seenRows[i], current)) return false;
      if (!checkAndMark(seenCols[j], current)) return false;
      if (!checkAndMark(seenBoxes[box], current)) return false;
    }
  }

  return true;
};

36_solution4_bitmasking.js

/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function (board) {
  const seenRows = new Array(9).fill(0);
  const seenCols = new Array(9).fill(0);
  const seenBoxes = new Array(9).fill(0);

  const checkAndMark = (masks, idx, bit) => {
    if ((masks[idx] & (1 << bit)) !== 0) return false;
    masks[idx] |= 1 << bit;
    return true;
  };

  for (let i = 0; i < 9; i++) {
    for (let j = 0; j < 9; j++) {
      if (board[i][j] === ".") continue;

      const current = board[i][j].charCodeAt(0) - "0".charCodeAt(0) - 1;
      const box = Math.floor(i / 3) * 3 + Math.floor(j / 3);

      if (!checkAndMark(seenRows, i, current)) return false;
      if (!checkAndMark(seenCols, j, current)) return false;
      if (!checkAndMark(seenBoxes, box, current)) return false;
    }
  }

  return true;
};

36_solution1_bruteforce.py

class Solution:
    def isValidSudoku(self, board: list[list[str]]) -> bool:
        seen = [False] * 9

        for i in range(9):
            for j in range(9):
                if board[i][j] == ".":
                    continue
                current = int(board[i][j]) - 1
                if seen[current]:
                    return False
                seen[current] = True
            seen = [False] * 9

        for j in range(9):
            for i in range(9):
                if board[i][j] == ".":
                    continue
                current = int(board[i][j]) - 1
                if seen[current]:
                    return False
                seen[current] = True
            seen = [False] * 9

        for rowOffset in range(0, 9, 3):
            for colOffset in range(0, 9, 3):
                for i in range(3):
                    for j in range(3):
                        val = board[i + rowOffset][j + colOffset]
                        if val == ".":
                            continue
                        current = int(val) - 1
                        if seen[current]:
                            return False
                        seen[current] = True
                seen = [False] * 9

        return True

36_solution2_onepass_offsets.py

class Solution:
    def isValidSudoku(self, board: list[list[str]]) -> bool:
        seenGrid = [False] * 9
        seenCol = [[False] * 9 for _ in range(9)]
        seenRow = [[False] * 9 for _ in range(9)]

        for rowOffset in range(0, 9, 3):
            for colOffset in range(0, 9, 3):
                for i in range(3):
                    for j in range(3):
                        val = board[i + rowOffset][j + colOffset]
                        if val == ".":
                            continue

                        current = int(val) - 1

                        if seenGrid[current]:
                            return False
                        seenGrid[current] = True

                        if seenRow[i + rowOffset][current]:
                            return False
                        seenRow[i + rowOffset][current] = True

                        if seenCol[j + colOffset][current]:
                            return False
                        seenCol[j + colOffset][current] = True

                seenGrid = [False] * 9

        return True

36_solution3_onepass_formula.py

class Solution:
    def isValidSudoku(self, board: list[list[str]]) -> bool:
        seenRows = [[False] * 9 for _ in range(9)]
        seenCols = [[False] * 9 for _ in range(9)]
        seenBoxes = [[False] * 9 for _ in range(9)]

        def checkAndMark(arr: list[bool], idx: int) -> bool:
            if arr[idx]:
                return False
            arr[idx] = True
            return True

        for i in range(9):
            for j in range(9):
                if board[i][j] == ".":
                    continue

                current = int(board[i][j]) - 1
                box = (i // 3) * 3 + (j // 3)

                if not checkAndMark(seenRows[i], current):
                    return False
                if not checkAndMark(seenCols[j], current):
                    return False
                if not checkAndMark(seenBoxes[box], current):
                    return False

        return True

36_solution4_bitmasking.py

class Solution:
    def isValidSudoku(self, board: list[list[str]]) -> bool:
        seenRows = [0] * 9
        seenCols = [0] * 9
        seenBoxes = [0] * 9

        def checkAndMark(masks: list[int], idx: int, bit: int) -> bool:
            if masks[idx] & (1 << bit):
                return False
            masks[idx] |= 1 << bit
            return True

        for i in range(9):
            for j in range(9):
                if board[i][j] == ".":
                    continue

                current = int(board[i][j]) - 1
                box = (i // 3) * 3 + (j // 3)

                if not checkAndMark(seenRows, i, current):
                    return False
                if not checkAndMark(seenCols, j, current):
                    return False
                if not checkAndMark(seenBoxes, box, current):
                    return False

        return True

36_solution1_bruteforce.rs

impl Solution {
    pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
        let mut seen = [false; 9];

        for i in 0..9 {
            for j in 0..9 {
                if board[i][j] == '.' {
                    continue;
                }
                let current = (board[i][j] as u8 - b'0' - 1) as usize;
                if seen[current] {
                    return false;
                }
                seen[current] = true;
            }
            seen = [false; 9];
        }

        for j in 0..9 {
            for i in 0..9 {
                if board[i][j] == '.' {
                    continue;
                }
                let current = (board[i][j] as u8 - b'0' - 1) as usize;
                if seen[current] {
                    return false;
                }
                seen[current] = true;
            }
            seen = [false; 9];
        }

        for row_offset in (0..9).step_by(3) {
            for col_offset in (0..9).step_by(3) {
                for i in 0..3 {
                    for j in 0..3 {
                        let val = board[i + row_offset][j + col_offset];
                        if val == '.' {
                            continue;
                        }
                        let current = (val as u8 - b'0' - 1) as usize;
                        if seen[current] {
                            return false;
                        }
                        seen[current] = true;
                    }
                }
                seen = [false; 9];
            }
        }

        true
    }
}

36_solution2_onepass_offsets.rs

impl Solution {
    pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
        let mut seen_grid = [false; 9];
        let mut seen_col = [[false; 9]; 9];
        let mut seen_row = [[false; 9]; 9];

        for row_offset in (0..9).step_by(3) {
            for col_offset in (0..9).step_by(3) {
                for i in 0..3 {
                    for j in 0..3 {
                        let val = board[i + row_offset][j + col_offset];
                        if val == '.' {
                            continue;
                        }

                        let current = (val as u8 - b'0' - 1) as usize;

                        if seen_grid[current] {
                            return false;
                        }
                        seen_grid[current] = true;

                        if seen_row[i + row_offset][current] {
                            return false;
                        }
                        seen_row[i + row_offset][current] = true;

                        if seen_col[j + col_offset][current] {
                            return false;
                        }
                        seen_col[j + col_offset][current] = true;
                    }
                }
                seen_grid = [false; 9];
            }
        }

        true
    }
}

36_solution3_onepass_formula.rs

impl Solution {
    pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
        let mut seen_rows = [[false; 9]; 9];
        let mut seen_cols = [[false; 9]; 9];
        let mut seen_boxes = [[false; 9]; 9];

        fn check_and_mark(arr: &mut [bool; 9], idx: usize) -> bool {
            if arr[idx] {
                return false;
            }
            arr[idx] = true;
            true
        }

        for i in 0..9 {
            for j in 0..9 {
                if board[i][j] == '.' {
                    continue;
                }

                let current = (board[i][j] as u8 - b'0' - 1) as usize;
                let bx = (i / 3) * 3 + (j / 3);

                if !check_and_mark(&mut seen_rows[i], current) {
                    return false;
                }
                if !check_and_mark(&mut seen_cols[j], current) {
                    return false;
                }
                if !check_and_mark(&mut seen_boxes[bx], current) {
                    return false;
                }
            }
        }

        true
    }
}

36_solution4_bitmasking.rs

impl Solution {
    pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
        let mut seen_rows = [0i32; 9];
        let mut seen_cols = [0i32; 9];
        let mut seen_boxes = [0i32; 9];

        fn check_and_mark(masks: &mut [i32; 9], idx: usize, bit: u8) -> bool {
            if masks[idx] & (1 << bit) != 0 {
                return false;
            }
            masks[idx] |= 1 << bit;
            true
        }

        for i in 0..9 {
            for j in 0..9 {
                if board[i][j] == '.' {
                    continue;
                }

                let current = board[i][j] as u8 - b'0' - 1;
                let bx = (i / 3) * 3 + (j / 3);

                if !check_and_mark(&mut seen_rows, i, current) {
                    return false;
                }
                if !check_and_mark(&mut seen_cols, j, current) {
                    return false;
                }
                if !check_and_mark(&mut seen_boxes, bx, current) {
                    return false;
                }
            }
        }

        true
    }
}