Anagram Check
PHP
Medium
4 views
Problem Description
Given two strings, check if they are anagrams (ignore spaces, case-insensitive).
Input Format
Two lines a and b.
Official Solution
<?php
$inputText=rtrim(stream_get_contents(STDIN));
if($inputText==='') exit;
$inputLines=preg_split('/\\R/', $inputText);
$a=strtolower(str_replace(' ','',$inputLines[0] ?? ''));
$b=strtolower(str_replace(' ','',$inputLines[1] ?? ''));
if(strlen($a)!==strlen($b)){ echo 'NO'; exit; }
$fa=[]; $fb=[];
for($i=0,$n=strlen($a);$i<$n;$i++){
$ca=$a[$i]; $cb=$b[$i];
if(!isset($fa[$ca])) $fa[$ca]=0;
if(!isset($fb[$cb])) $fb[$cb]=0;
$fa[$ca]++; $fb[$cb]++;
}
ksort($fa); ksort($fb);
echo ($fa==$fb) ? 'YES' : 'NO';
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!