Browse Source

Add phoneNumberLetterPermutations script

master
Tovi Jaeschke-Rogers 2 years ago
parent
commit
5f5e689874
1 changed files with 49 additions and 0 deletions
  1. +49
    -0
      phoneNumberLetterPermutations.php

+ 49
- 0
phoneNumberLetterPermutations.php View File

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

Loading…
Cancel
Save