ShortestWordDistance::shortestDistance()   B
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 20
rs 8.8333
cc 7
nc 10
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ShortestWordDistance
8
{
9
    public static function shortestDistance(array &$words, string $s1, string $s2): int
10
    {
11
        $ans = PHP_INT_MAX;
12
        if (empty($words)) {
13
            return $ans;
14
        }
15
        [$n, $s, $t] = [count($words), -1, -1];
16
        for ($i = 0; $i < $n; $i++) {
17
            if ($words[$i] === $s1) {
18
                $s = $i;
19
            }
20
            if ($words[$i] === $s2) {
21
                $t = $i;
22
            }
23
            if ($s !== -1 && $t !== -1) {
24
                $ans = min($ans, abs($s - $t));
25
            }
26
        }
27
28
        return $ans;
29
    }
30
}
31