| Total Complexity | 18 |
| Total Lines | 64 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class ValidPalindrome |
||
| 8 | { |
||
| 9 | public static function isPalindrome(string $s): bool |
||
| 10 | { |
||
| 11 | if (0 === ($n = strlen($s))) { |
||
| 12 | return true; |
||
| 13 | } |
||
| 14 | $ans = []; |
||
| 15 | for ($i = 0; $i < $n; $i++) { |
||
| 16 | // Can also use ctype_alnum instead. |
||
| 17 | if (preg_match('/^[a-zA-Z0-9]$/', $s[$i])) { |
||
| 18 | array_push($ans, strtolower($s[$i])); |
||
| 19 | } |
||
| 20 | } |
||
| 21 | |||
| 22 | return $ans === array_reverse($ans); |
||
| 23 | } |
||
| 24 | |||
| 25 | public static function isPalindrome2(string $s): bool |
||
| 26 | { |
||
| 27 | if (empty($s)) { |
||
| 28 | return true; |
||
| 29 | } |
||
| 30 | [$i, $j] = [0, strlen($s) - 1]; |
||
| 31 | while ($i < $j) { |
||
| 32 | if (!ctype_alnum($s[$i])) { |
||
| 33 | $i++; |
||
| 34 | } elseif (!ctype_alnum($s[$j])) { |
||
| 35 | $j--; |
||
| 36 | } elseif (strtolower($s[$i]) !== strtolower($s[$j])) { |
||
| 37 | return false; |
||
| 38 | } else { |
||
| 39 | $i++; |
||
| 40 | $j--; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | return true; |
||
| 45 | } |
||
| 46 | |||
| 47 | public static function isPalindrome3(string $s): bool |
||
| 71 | } |
||
| 72 | } |
||
| 73 |