Test Failed
Push — master ( 17c827...08abcd )
by Jinyun
02:46
created

FirstUniqueCharacterInAString::firstUniqChar()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
rs 9.6111
cc 5
nc 7
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class FirstUniqueCharacterInAString
8
{
9
    public static function firstUniqChar(string $s): int
10
    {
11
        if (empty($s)) {
12
            return 0;
13
        }
14
        $n = strlen($s);
15
        $map = array_fill(0, 26, 0);
16
        for ($i = 0; $i < $n; $i++) {
17
            $map[ord($s[$i]) - 26] = ($map[ord($s[$i]) - 26] ?? 0) + 1;
18
        }
19
        for ($i = 0; $i < $n; $i++) {
20
            if ($map[ord($s[$i]) - 26] === 1) {
21
                return $i;
22
            }
23
        }
24
25
        return -1;
26
    }
27
}
28