Longest Palindromic Substring Length

Longest Palindromic Substring Length

Hard PHP PHP Strings 31 views
Explanation Complexity

Problem Statement

Print the length of the longest palindromic substring (expand-around-center).

Input Format

One line string s.

Output Format

One integer length.

Example

babad
3

Constraints

|s|

Input / Output Format

Input Format
One line string s.
Output Format
One integer length.
Constraints
|s|

Examples

Input:
babad
Output:
3

Example Solution (Public)

PHP
<?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;
?>

Official Solution Code

<?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;
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.