Palindrome Function

Palindrome Function

Easy PHP PHP Functions 30 views
Explanation Complexity

Problem Statement

Create a function isPalindrome(s) that ignores spaces and case. Print YES or NO.

Input Format

One line string s.

Output Format

YES or NO.

Example

Never odd or even
YES

Constraints

|s|

Input / Output Format

Input Format
One line string s.
Output Format
YES or NO.
Constraints
|s|

Examples

Input:
Never odd or even
Output:
YES

Example Solution (Public)

PHP
<?php
$inputText=rtrim(stream_get_contents(STDIN));
if($inputText==='') exit;
function clean($t){
  $t=strtolower($t);
  $out='';
  for($i=0,$n=strlen($t);$i<$n;$i++){
    $ch=$t[$i];
    if($ch===' ') continue;
    $out.=$ch;
  }
  return $out;
}
$t=clean($inputText);
$l=0; $r=strlen($t)-1; $ok=true;
while($l<$r){
  if($t[$l]!==$t[$r]){ $ok=false; break; }
  $l++; $r--;
}
echo $ok ? 'YES' : 'NO';
?>

Official Solution Code

<?php
$inputText=rtrim(stream_get_contents(STDIN));
if($inputText==='') exit;
function clean($t){
  $t=strtolower($t);
  $out='';
  for($i=0,$n=strlen($t);$i<$n;$i++){
    $ch=$t[$i];
    if($ch===' ') continue;
    $out.=$ch;
  }
  return $out;
}
$t=clean($inputText);
$l=0; $r=strlen($t)-1; $ok=true;
while($l<$r){
  if($t[$l]!==$t[$r]){ $ok=false; break; }
  $l++; $r--;
}
echo $ok ? 'YES' : 'NO';
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.