<?php function lookAndSay(int $n): string { if ($n <= 1) return '1'; $r = $lastSeq = '1'; while($n-- > 0) $r .= ' '.($lastSeq = tokenize($lastSeq)); return $r; } function tokenize(string $str): string { $r = ''; $c = 1; for ($i = 0; $i < strlen($str); $i++) { if($str[$i] == ($str[$i + 1] ?? '')) { $c++; continue; } ($r .= $c.$str[$i]) && ($c = 1); } return $r; } echo lookAndSay(3);
You have javascript disabled. You will not be able to edit any code.