2) { $ans = $c; } else if ($c == 2){ if ($h[0] === $h[1]) { $ans = 1; } } 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); }