|
@ -0,0 +1,49 @@ |
|
|
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Solution |
|
|
|
|
|
{ |
|
|
|
|
|
const letters = [ |
|
|
|
|
|
2 => ['a','b','c'], |
|
|
|
|
|
3 => ['d','e','f'], |
|
|
|
|
|
4 => ['g','h','i'], |
|
|
|
|
|
5 => ['j','k','l'], |
|
|
|
|
|
6 => ['m','n','o'], |
|
|
|
|
|
7 => ['p','q','r','s'], |
|
|
|
|
|
8 => ['t','u','v'], |
|
|
|
|
|
9 => ['w','x','y','z'] |
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param mixed $digits |
|
|
|
|
|
* @access public |
|
|
|
|
|
* @return array |
|
|
|
|
|
*/ |
|
|
|
|
|
public static function letterCombinations($digits): array |
|
|
|
|
|
{ |
|
|
|
|
|
if ($digits === '') { |
|
|
|
|
|
return []; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$lookupResult = []; |
|
|
|
|
|
$x = 0; |
|
|
|
|
|
while (count($lookupResult) === 0) { |
|
|
|
|
|
$lookupResult = self::letters[$digits[$x]] ?? []; |
|
|
|
|
|
$x++; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for ($i = 1; $i < strlen($digits); $i++) { |
|
|
|
|
|
$temp = []; |
|
|
|
|
|
|
|
|
|
|
|
foreach (self::letters[$digits[$i]] as $char) { |
|
|
|
|
|
foreach ($lookupResult as $r) { |
|
|
|
|
|
$temp[] = $r . $char; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$lookupResult = $temp; |
|
|
|
|
|
} |
|
|
|
|
|
return $lookupResult; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |