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

FirstUniqueCharacterInAString   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 19
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A firstUniqChar() 0 17 5
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