ValidSudoku   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 17
c 2
b 0
f 0
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B isValidSudoku() 0 25 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ValidSudoku
8
{
9
    public static function isValidSudoku(array $board): bool
10
    {
11
        [$m, $n] = [count($board), count($board[0])];
12
        if ($m === 0 || $n === 0) {
13
            return false;
14
        }
15
        $helper = static function (array &$array, int $k, int $v) {
16
            $array[$k][$v] = isset($array[$k][$v]) ? $array[$k][$v] + 1 : 1;
17
        };
18
        $row = $col = $box = array_fill(0, $m, array_fill(0, $n, 0));
19
        for ($i = 0; $i < $m; $i++) {
20
            for ($j = 0; $j < $n; $j++) {
21
                if ('.' !== $v = $board[$i][$j]) {
22
                    [$v, $k] = [(int) $v, (int) (floor($i / 3) * 3 + floor($j / 3))];
23
                    $helper($row, $i, $v);
24
                    $helper($col, $j, $v);
25
                    $helper($box, $k, $v);
26
                    if ($row[$i][$v] > 1 || $col[$j][$v] > 1 || $box[$k][$v] > 1) {
27
                        return false;
28
                    }
29
                }
30
            }
31
        }
32
33
        return true;
34
    }
35
}
36