Conditions | 6 |
Paths | 7 |
Total Lines | 17 |
Code Lines | 10 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
26 | public static function longestPalindrome2(string $s): int |
||
27 | { |
||
28 | if (empty($s)) { |
||
29 | return 0; |
||
30 | } |
||
31 | [$ans, $n] = [0, strlen($s)]; |
||
32 | for ($i = 0; $i < $n; $i++) { |
||
33 | $map[$s[$i]] = ($map[$s[$i]] ?? 0) + 1; |
||
34 | } |
||
35 | foreach ($map as $val) { |
||
36 | $ans += (int)($val / 2) * 2; |
||
37 | if ($ans % 2 === 0 && $val % 2 === 1) { |
||
38 | $ans++; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | return $ans; |
||
43 | } |
||
45 |