Conditions | 12 |
Paths | 89 |
Total Lines | 38 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
85 |