MinimumWindowSubstring::minWindow()   C
last analyzed

Complexity

Conditions 12
Paths 89

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 38
rs 6.9666
cc 12
nc 89
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class MinimumWindowSubstring
8
{
9
    public static function minWindow(string $s, string $t): string
10
    {
11
        [$m, $n, $map, $win] = [strlen($s), strlen($t), [], []];
12
        if ($m === 0 || $n === 0) {
13
            return '';
14
        }
15
        for ($i = 0; $i < $n; $i++) {
16
            $k = $t[$i];
17
            $map[$k] = ($map[$k] ?? 0) + 1;
18
        }
19
        $left = $right = $match = $start = 0;
20
        $len = PHP_INT_MAX;
21
        while ($right < $m) {
22
            $c = $s[$right];
23
            $right++;
24
            if (isset($map[$c])) {
25
                $win[$c] = ($win[$c] ?? 0) + 1;
26
                if ($win[$c] === $map[$c]) {
27
                    $match++;
28
                }
29
            }
30
            while ($match === count($map)) {
31
                if ($right - $left < $len) {
32
                    $start = $left;
33
                    $len = $right - $left;
34
                }
35
                $d = $s[$left];
36
                $left++;
37
                if (isset($map[$d])) {
38
                    if ($win[$d] === $map[$d]) {
39
                        $match--;
40
                    }
41
                    $win[$d]--;
42
                }
43
            }
44
        }
45
46
        return $len === PHP_INT_MAX ? '' : substr($s, $start, $len);
47
    }
48
49
    public static function minWindow2(string $s, string $t): string
50
    {
51
        [$m, $n, $map] = [strlen($s), strlen($t), []];
52
        if ($m === 0 || $n === 0) {
53
            return '';
54
        }
55
        for ($i = 0; $i < $n; $i++) {
56
            $k = $t[$i];
57
            $map[$k] = ($map[$k] ?? 0) + 1;
58
        }
59
        $left = $right = $start = 0;
60
        $len = PHP_INT_MAX;
61
        while ($right < $m) {
62
            $c = $s[$right];
63
            $right++;
64
            if (isset($map[$c]) && $map[$c] > 0) {
65
                $n--;
66
            }
67
            $map[$c] = ($map[$c] ?? 0) - 1;
68
            while ($n === 0) {
69
                if ($right - $left < $len) {
70
                    $len = $right - $left;
71
                    $start = $left;
72
                }
73
                $d = $s[$left];
74
                $left++;
75
                $map[$d] = ($map[$d] ?? 0) + 1;
76
                if ($map[$d] > 0) {
77
                    $n++;
78
                }
79
            }
80
        }
81
82
        return $len === PHP_INT_MAX ? '' : substr($s, $start, $len);
83
    }
84
}
85