You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

49 lines
1.0 KiB

<?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;
}
}