File tree 3 files changed +102
-0
lines changed
3 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 252
252
* [ FibonacciNumberRecursive] ( Recursive/FibonacciNumberRecursive.js )
253
253
* [ FloodFill] ( Recursive/FloodFill.js )
254
254
* [ KochSnowflake] ( Recursive/KochSnowflake.js )
255
+ * [ LetterCombination] ( Recursive/LetterCombination.js )
255
256
* [ Palindrome] ( Recursive/Palindrome.js )
256
257
* [ SubsequenceRecursive] ( Recursive/SubsequenceRecursive.js )
257
258
* [ TowerOfHanoi] ( Recursive/TowerOfHanoi.js )
Original file line number Diff line number Diff line change
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 }
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments