-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathLetterCombinationPhoneNumber_17.java
58 lines (47 loc) · 1.86 KB
/
LetterCombinationPhoneNumber_17.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.leetcode.problems.medium;
import java.util.ArrayList;
import java.util.List;
/**
* @author neeraj on 01/09/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class LetterCombinationPhoneNumber_17 {
private static String[] MAPPINGS = {
"0",
"1",
"ABC", // Represent Digit 2 in Phone Number
"DEF", // Represent Digit 3 in Phone Number
"GHI", // Represent Digit 4 in Phone Number
"JKL", // Represent Digit 5 in Phone Number
"MNO", // Represent Digit 6 in Phone Number
"PQRS", // Represent Digit 7 in Phone Number
"TUV", // Represent Digit 8 in Phone Number
"WXYZ" // Represent Digit 9 in Phone Number
};
public static void main(String[] args) {
letterCombinations("23");
}
public static List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<>();
combination(0, digits, "", result);
System.out.println(result);
return result;
}
public static void combination(int counter,
String digits,
String currentCombination,
List<String> result) {
if (counter == digits.length()) {
result.add(currentCombination);
return;
}
String mappingAtCounter = MAPPINGS[digits.charAt(counter) - '0'];
for (int i = 0; i < mappingAtCounter.length(); i++) {
currentCombination = currentCombination.concat("" + mappingAtCounter.charAt(i));
combination(counter + 1, digits, currentCombination, result);
currentCombination = new StringBuffer(currentCombination)
.deleteCharAt(currentCombination.length() - 1).toString();
}
}
}