< Summary

Information
Class: LeetCode.Algorithms.ValidSudoku.ValidSudokuBitmask
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidSudoku\ValidSudokuBitmask.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 68
Line coverage: 100%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsValidSudoku(...)83.33%1212100%
HasSeen(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidSudoku\ValidSudokuBitmask.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.ValidSudoku;
 13
 14/// <inheritdoc />
 15public class ValidSudokuBitmask : ValidSudokuBase
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="board"></param>
 22    /// <returns></returns>
 23    public override bool IsValidSudoku(char[][] board)
 324    {
 325        Span<int> rows = stackalloc int[N];
 326        Span<int> columns = stackalloc int[N];
 327        Span<int> boxes = stackalloc int[N];
 28
 3229        for (var rowIndex = 0; rowIndex < N; rowIndex++)
 1530        {
 1531            var row = board[rowIndex];
 32
 27833            for (var columnIndex = 0; columnIndex < N; columnIndex++)
 12634            {
 12635                var cell = row[columnIndex];
 36
 12637                if (cell == EmptyCell)
 8238                {
 8239                    continue;
 40                }
 41
 4442                var cellIndex = GetCellIndex(cell);
 43
 4444                var bit = 1 << cellIndex;
 45
 4446                var boxIndex = GetBoxIndex(rowIndex, columnIndex);
 47
 4448                if (HasSeen(rows[rowIndex], bit) ||
 4449                    HasSeen(columns[columnIndex], bit) ||
 4450                    HasSeen(boxes[boxIndex], bit))
 251                {
 252                    return false;
 53                }
 54
 4255                rows[rowIndex] |= bit;
 4256                columns[columnIndex] |= bit;
 4257                boxes[boxIndex] |= bit;
 4258            }
 1359        }
 60
 161        return true;
 362    }
 63
 64    private static bool HasSeen(int mask, int bit)
 13265    {
 13266        return (mask & bit) != 0;
 13267    }
 68}