File tree 2 files changed +48
-0
lines changed
2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 14
14
8|[ String to Integer (atoi)] ( ./0008-string-to-integer-atoi.js ) |Medium|
15
15
10|[ Regular Expression Matching] ( ./0010-regular-expression-matching.js ) |Hard|
16
16
14|[ Longest Common Prefix] ( ./0014-longest-common-prefix.js ) |Easy|
17
+ 17|[ Letter Combinations of a Phone Number] ( ./0017-letter-combinations-of-a-phone-number.js ) |Medium|
17
18
27|[ Remove Element] ( ./0027-remove-element.js ) |Easy|
18
19
31|[ Next Permutation] ( ./0031-next-permutation.js ) |Medium|
19
20
36|[ Valid Sudoku] ( ./0036-valid-sudoku.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 17. Letter Combinations of a Phone Number
3
+ * https://leetcode.com/problems/letter-combinations-of-a-phone-number/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given a string containing digits from 2-9 inclusive, return all possible
7
+ * letter combinations that the number could represent. Return the answer
8
+ * in any order.
9
+ *
10
+ * A mapping of digit to letters (just like on the telephone buttons) is
11
+ * given below. Note that 1 does not map to any letters.
12
+ */
13
+
14
+ /**
15
+ * @param {string } digits
16
+ * @return {string[] }
17
+ */
18
+ var letterCombinations = function ( digits ) {
19
+ if ( ! digits || ! digits . length ) return [ ] ;
20
+
21
+ const map = {
22
+ 2 : 'abc' ,
23
+ 3 : 'def' ,
24
+ 4 : 'ghi' ,
25
+ 5 : 'jkl' ,
26
+ 6 : 'mno' ,
27
+ 7 : 'pqrs' ,
28
+ 8 : 'tuv' ,
29
+ 9 : 'wxyz'
30
+ } ;
31
+
32
+ if ( digits . length === 1 ) {
33
+ return map [ digits ] . split ( '' ) ;
34
+ }
35
+
36
+ const result = [ ] ;
37
+ const group1 = letterCombinations ( digits . substr ( 0 , 1 ) ) ;
38
+ const group2 = letterCombinations ( digits . substr ( 1 ) ) ;
39
+
40
+ for ( let i = 0 ; i < group1 . length ; i ++ ) {
41
+ for ( let j = 0 ; j < group2 . length ; j ++ ) {
42
+ result . push ( group1 [ i ] + group2 [ j ] ) ;
43
+ }
44
+ }
45
+
46
+ return result ;
47
+ } ;
You can’t perform that action at this time.
0 commit comments