-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidPalindrome.php
More file actions
48 lines (34 loc) · 906 Bytes
/
validPalindrome.php
File metadata and controls
48 lines (34 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。说明:本题中,我们将空字符串定义为有效的回文串。
* @param $s
*/
function isPalindrome($s) {
if(!$s){
return true;
}
$s = trim($s);
$len = strlen($s);
$start = 0;
$end = $len - 1;
// "A man, a plan, a canal: Panama"
while ($start <= $end){
if($s[$start] === ' ' || !ctype_alnum($s[$start])){
$start++;
continue;
}
if(!$s[$end] === ' ' || !ctype_alnum($s[$end])){
$end--;
continue;
}
if(strtolower($s[$start]) !== strtolower($s[$end])){
return false;
}
$start++;
$end--;
}
return true;
}
$s = "A man, a plan, a canal: Panama";
$s = "0P";
print_r(isPalindrome($s));