Conditions | 7 |
Paths | 10 |
Total Lines | 20 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 | } |
||
31 |