Skip to content

Commit 63a3394

Browse files
authored
algorithm: letter combinations (#1209)
1 parent ce9e294 commit 63a3394

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
* [FibonacciNumberRecursive](Recursive/FibonacciNumberRecursive.js)
253253
* [FloodFill](Recursive/FloodFill.js)
254254
* [KochSnowflake](Recursive/KochSnowflake.js)
255+
* [LetterCombination](Recursive/LetterCombination.js)
255256
* [Palindrome](Recursive/Palindrome.js)
256257
* [SubsequenceRecursive](Recursive/SubsequenceRecursive.js)
257258
* [TowerOfHanoi](Recursive/TowerOfHanoi.js)

Diff for: Recursive/LetterCombination.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
*
3+
* Letter Combinations of a Phone Number
4+
*
5+
* Given a string containing digits from 2-9 inclusive,
6+
* return all possible letter combinations that the number could represent.
7+
* Return the answer in any order.
8+
9+
* A mapping of digits to letters (just like on the telephone buttons) is given below.
10+
* Note that 1 does not map to any letters.
11+
* More info: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
12+
*/
13+
14+
/*
15+
* @param {string} digits
16+
* @returns {string[]} all the possible combinations
17+
*/
18+
19+
const letterCombinations = (digits) => {
20+
const length = digits?.length
21+
const result = []
22+
if (!length) {
23+
return result
24+
}
25+
const digitMap = {
26+
2: 'abc',
27+
3: 'def',
28+
4: 'ghi',
29+
5: 'jkl',
30+
6: 'mno',
31+
7: 'pqrs',
32+
8: 'tuv',
33+
9: 'wxyz'
34+
}
35+
36+
const combinations = (index, combination) => {
37+
let letter
38+
let letterIndex
39+
if (index >= length) {
40+
result.push(combination)
41+
return
42+
}
43+
const digit = digitMap[digits[index]]
44+
letterIndex = 0
45+
while ((letter = digit[letterIndex++])) {
46+
combinations(index + 1, combination + letter)
47+
}
48+
}
49+
combinations(0, '')
50+
return result
51+
}
52+
53+
export { letterCombinations }

Diff for: Recursive/test/LetterCombination.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { letterCombinations } from '../LetterCombination'
2+
3+
describe('Letter Combinations', () => {
4+
it('should return empty array if provided string is not valid', () => {
5+
const result = letterCombinations('')
6+
expect(Array.isArray(result)).toBe(true)
7+
expect(result.length).toBe(0)
8+
})
9+
10+
it('should return empty array if provided string is empty', () => {
11+
const result = letterCombinations(null)
12+
expect(Array.isArray(result)).toBe(true)
13+
expect(result.length).toBe(0)
14+
})
15+
16+
it('should return letter combination of 234', () => {
17+
const result = letterCombinations('234')
18+
expect(result).toEqual([
19+
'adg',
20+
'adh',
21+
'adi',
22+
'aeg',
23+
'aeh',
24+
'aei',
25+
'afg',
26+
'afh',
27+
'afi',
28+
'bdg',
29+
'bdh',
30+
'bdi',
31+
'beg',
32+
'beh',
33+
'bei',
34+
'bfg',
35+
'bfh',
36+
'bfi',
37+
'cdg',
38+
'cdh',
39+
'cdi',
40+
'ceg',
41+
'ceh',
42+
'cei',
43+
'cfg',
44+
'cfh',
45+
'cfi'
46+
])
47+
})
48+
})

0 commit comments

Comments
 (0)