ShortestWordDistance   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 2
b 0
f 0
dl 0
loc 22
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B shortestDistance() 0 20 7
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