Longest Palindromic Substring Length
PHP
Hard
5 views
Problem Description
Print the length of the longest palindromic substring (expand-around-center).
Input Format
One line string s.
Output Format
One integer length.
Official Solution
<?php
$inputText=rtrim(stream_get_contents(STDIN));
if($inputText==='') exit;
$n=strlen($inputText);
$best=1;
for($c=0;$c<$n;$c++){
$l=$c; $r=$c;
while($l>=0 && $r<$n && $inputText[$l]===$inputText[$r]){ $best=max($best,$r-$l+1); $l--; $r++; }
$l=$c; $r=$c+1;
while($l>=0 && $r<$n && $inputText[$l]===$inputText[$r]){ $best=max($best,$r-$l+1); $l--; $r++; }
}
echo $best;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!