class Solution {

/**
* @param String $s
* @return String
*/
function longestPalindrome($s) {
//一个字母重复N编的情况
if (count(array_count_values(str_split($s))) == 1) {
return $s;
}
$subS = '';
//转为有中心结构
$s = '#'.implode("#", str_split($s)).'#';
for ($i = 0; $i < strlen($s); $i++) {
$mid_index = $i;
$left_index = $mid_index - 1;
$right_index = $mid_index + 1;
$flag = true;
while ($flag) {
if ($left_index < 0) {
$flag = false;
if (strlen(substr($s, 0, $right_index)) > strlen($subS)) {
$subS = substr($s, 0, $right_index);
}
break;
}
if ($s[$left_index] == $s[$right_index]) {
//有中心结构
$left_index -= 1;
$right_index += 1;
} else {
$flag = false;
if (strlen(substr($s, $left_index + 1, ($right_index - $left_index - 1))) > strlen($subS)) {
$subS = substr($s, $left_index + 1, ($right_index - $left_index - 1));
}
break;
}
}
}
return str_ireplace('#', '', $subS);
}
}


*****************************只要思想不滑坡,办法总比困难多*****************************