// 按键映射
function keyMapping(str) {
if (!str) {
return '';
}
// 获取两个字符串的笛卡尔积
function cartesianProduct(str1, str2) {
if (str1.length === 0) return str2;
if (str2.length === 0) return str1;
let result = [];
for (let i = 0; i < str1.length; i++) {
for (let j = 0; j < str2.length; j++) {
result.push(str1[i] + str2[j]);
}
}
return result;
}
let keyArray = [, , 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; // 0-9
return str
.split('')
.map((d) => keyArray[d])
.reduce(cartesianProduct, []);
}
console.log(keyMapping('23')); // ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']
01-25
185
08-04
617