跳到主要内容

是否为回文字符串

力扣题库-125

问题描述

回文串是指正读反读都能读通的句子,如“我为人人,人人为我”。

/**
* 是否为回文字符串
* ```说明
* 只考虑字母和数字字符,忽略字母大小写。
* ```
* @param string $string
* @return boolean
*/
function isPalindrome ($string = ‘’) {
preg_match_all('/[a-zA-Z0-9]/i', $string, $matches);
if (!isset($matches[0])) {
return false;
}
$ascending = $matches[0];
$descending = $ascending;
krsort($descending);
$left = join('', $ascending);
$right = join('', $descending);
if (strcasecmp($left, $right) === 0) {
return true;
} else {
return false;
}
}