First, let's look at this figure, which is similar to the buttons on the desktop of an old-fashioned tablet phone. First, let's look at this figure, which is similar to the buttons on the desktop of an old-fashioned tablet phone.
Step 1: Create a ing based on the relationship between the number key and the corresponding letter.
var map = { 1 : [ 1 ], 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" ], 0 : [ 0 ] };
Step 2: use recursion to solve the permutation and combination
function telephoneWords(digitString) { var array = []; var result = []; digitString.split("").forEach(function(e) { array.push(map[e]); }) var traverse = function foo(from, to) { if (to.length < 4) { var cur = from.shift(); for (var i = 0; i < cur.length; i++) { var newTo = to.slice(0); newTo.push(cur[i]); var newFrom = from.slice(0); foo(newFrom, newTo); } } else { result.push(to.join("")); } }; traverse(array, []); return result; }
The above are JavaScript interesting questions: arrange and combine the actual content. For more information, see the PHP Chinese website (www.php1.cn )!