2) { $ans = $ans - count($out[0]) * 2; } echo $ans . PHP_EOL; function scanning($from, $to, &$a) { for($i=0;$i < count($to); $i++) { $o = array_intersect_assoc($from, $to); $l = array(); foreach((array)$o as $k => $v) { $l[] = $v; if (!isset($o[$k+1])) { if (implode($l) === strrev(implode($l)) ) { if (strpos(implode($to), implode($l)) !== false) { $a[] = $l; } } $l = array(); } } array_shift($from); array_merge($from); } arsort($a); }