| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.ValidSudoku; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ValidSudokuBruteForce : 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) |
| 3 | 24 | | { |
| 3 | 25 | | Span<bool> seen = stackalloc bool[N]; |
| | 26 | |
|
| 3 | 27 | | return AreValidRows(seen, board) && |
| 3 | 28 | | AreValidColumns(seen, board) && |
| 3 | 29 | | AreValidBoxes(seen, board); |
| 3 | 30 | | } |
| | 31 | |
|
| | 32 | | private static bool AreValidRows(scoped Span<bool> seen, char[][] board) |
| 3 | 33 | | { |
| 60 | 34 | | for (var row = 0; row < N; row++) |
| 27 | 35 | | { |
| 540 | 36 | | for (var column = 0; column < N; column++) |
| 243 | 37 | | { |
| 243 | 38 | | if (!TryInsert(seen, board[row][column])) |
| 0 | 39 | | { |
| 0 | 40 | | return false; |
| | 41 | | } |
| 243 | 42 | | } |
| | 43 | |
|
| 27 | 44 | | seen.Clear(); |
| 27 | 45 | | } |
| | 46 | |
|
| 3 | 47 | | return true; |
| 3 | 48 | | } |
| | 49 | |
|
| | 50 | | private static bool AreValidColumns(scoped Span<bool> seen, char[][] board) |
| 3 | 51 | | { |
| 42 | 52 | | for (var column = 0; column < N; column++) |
| 19 | 53 | | { |
| 368 | 54 | | for (var row = 0; row < N; row++) |
| 166 | 55 | | { |
| 166 | 56 | | if (!TryInsert(seen, board[row][column])) |
| 1 | 57 | | { |
| 1 | 58 | | return false; |
| | 59 | | } |
| 165 | 60 | | } |
| | 61 | |
|
| 18 | 62 | | seen.Clear(); |
| 18 | 63 | | } |
| | 64 | |
|
| 2 | 65 | | return true; |
| 3 | 66 | | } |
| | 67 | |
|
| | 68 | | private static bool AreValidBoxes(scoped Span<bool> seen, char[][] board) |
| 2 | 69 | | { |
| 10 | 70 | | for (var boxRow = 0; boxRow < N; boxRow += 3) |
| 4 | 71 | | { |
| 28 | 72 | | for (var boxColumn = 0; boxColumn < N; boxColumn += 3) |
| 11 | 73 | | { |
| 86 | 74 | | for (var row = boxRow; row < boxRow + 3; row++) |
| 33 | 75 | | { |
| 262 | 76 | | for (var column = boxColumn; column < boxColumn + 3; column++) |
| 99 | 77 | | { |
| 99 | 78 | | if (!TryInsert(seen, board[row][column])) |
| 1 | 79 | | { |
| 1 | 80 | | return false; |
| | 81 | | } |
| 98 | 82 | | } |
| 32 | 83 | | } |
| | 84 | |
|
| 10 | 85 | | seen.Clear(); |
| 10 | 86 | | } |
| 3 | 87 | | } |
| | 88 | |
|
| 1 | 89 | | return true; |
| 2 | 90 | | } |
| | 91 | |
|
| | 92 | | private static bool TryInsert(scoped Span<bool> seen, int cell) |
| 508 | 93 | | { |
| 508 | 94 | | if (cell == EmptyCell) |
| 349 | 95 | | { |
| 349 | 96 | | return true; |
| | 97 | | } |
| | 98 | |
|
| 159 | 99 | | var cellIndex = GetCellIndex(cell); |
| | 100 | |
|
| 159 | 101 | | if (seen[cellIndex]) |
| 2 | 102 | | { |
| 2 | 103 | | return false; |
| | 104 | | } |
| | 105 | |
|
| 157 | 106 | | seen[cellIndex] = true; |
| | 107 | |
|
| 157 | 108 | | return true; |
| 508 | 109 | | } |
| | 110 | | } |