-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path17.java
More file actions
176 lines (160 loc) · 6.23 KB
/
17.java
File metadata and controls
176 lines (160 loc) · 6.23 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
__________________________________________________________________________________________________
0ms
class Solution {
private static final String[] KEYS = {"","","abc","def","ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
List<String> ret = new LinkedList<>();
if (digits == null || digits.length() < 1) {
return ret;
}
combination("",digits,0,ret);
return ret;
}
public void combination(String prefix, String digits, int offset, List<String> ret) {
if (offset >= digits.length()) {
ret.add(prefix);
return;
}
String letters = KEYS[ digits.charAt(offset) - '0' ];
for (int i=0; i<letters.length(); i++) {
combination(prefix+letters.charAt(i), digits, offset+1, ret);
}
}
}
__________________________________________________________________________________________________
1ms
class Solution {
List<String> ans = new ArrayList<>();
Map<String, List<String>> map = new HashMap<String, List<String>>() {{
put("2", Arrays.asList("a", "b", "c"));
put("3", Arrays.asList("d", "e", "f"));
put("4", Arrays.asList("g", "h", "i"));
put("5", Arrays.asList("j", "k", "l"));
put("6", Arrays.asList("m", "n", "o"));
put("7", Arrays.asList("p", "q", "r", "s"));
put("8", Arrays.asList("t", "u", "v"));
put("9", Arrays.asList("w", "x", "y", "z"));
}};
public List<String> letterCombinations(String digits) {
if (digits.length() == 0) {
return ans;
}
letterCombinationHelper(digits, "", 0);
return ans;
}
private void letterCombinationHelper(String digits, String s, int i) {
if (i == digits.length()) {
ans.add(s);
return;
}
for (String t : map.get(digits.substring(i, i + 1))) {
letterCombinationHelper(digits, s + t, i + 1);
}
}
}
__________________________________________________________________________________________________
2ms
class Solution {
private final Map<Character, List<Character>> intCharMap;
public Solution () {
intCharMap = new HashMap<>(10);
intCharMap.put('2', Arrays.asList(new Character[]{'a', 'b', 'c'}));
intCharMap.put('3', Arrays.asList(new Character[]{'d', 'e', 'f'}));
intCharMap.put('4', Arrays.asList(new Character[]{'g', 'h', 'i'}));
intCharMap.put('5', Arrays.asList(new Character[]{'j', 'k', 'l'}));
intCharMap.put('6', Arrays.asList(new Character[]{'m', 'n', 'o'}));
intCharMap.put('7', Arrays.asList(new Character[]{'p', 'q', 'r', 's'}));
intCharMap.put('8', Arrays.asList(new Character[]{'t', 'u', 'v'}));
intCharMap.put('9', Arrays.asList(new Character[]{'w', 'x', 'y', 'z'}));
}
public List<String> letterCombinations(String digits) {
Map<Integer, List<Character>> digitsLookedUp = new HashMap<>();
for (int i=0; i<digits.length(); i++) {
digitsLookedUp.put(i, intCharMap.get(digits.charAt(i)));
}
int lengthOfWord = digitsLookedUp.size();
List<String> toReturn = new ArrayList<>();
for (int i=0; i<lengthOfWord; i++) {
List<Character> chars = digitsLookedUp.get(i);
if (toReturn.size()==0) {
for(char c : chars) {
toReturn.add(String.valueOf(c));
}
} else {
List<String> toTempReturn = new ArrayList<>();
for (String s : toReturn) {
for (Character c : chars) {
toTempReturn.add(s + c);
}
}
toReturn = toTempReturn;
}
}
return toReturn;
}
}
__________________________________________________________________________________________________
34516 kb
class Solution {
Map<Character, String> phone = new HashMap<Character, String>() {{
put('2', "abc");
put('3', "def");
put('4', "ghi");
put('5', "jkl");
put('6', "mno");
put('7', "pqrs");
put('8', "tuv");
put('9', "wxyz");
}};
List<String> output;
public void backtrack(String combination, String next_digits){
// if there is no more digits to check
if(next_digits.length() == 0){
// the combination is done
output.add(combination);
}
// if there are still digits to check
else {
// iterate over all letters which map
// the next available digit
char digit = next_digits.charAt(0);
String letters = phone.get(digit);
for(int i = 0 ; i < letters.length() ; i++){
char letter = phone.get(digit).charAt(i);
// append the current letter to the combination
// and proceed to the next digits
backtrack(combination + letter, next_digits.substring(1));
}
}
}
public List<String> letterCombinations(String digits) {
output = new ArrayList<>();
if(digits.length() != 0){
backtrack("", digits);
}
return output;
}
}
__________________________________________________________________________________________________
34548 kb
class Solution {
private String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
LinkedList<String> ans = new LinkedList<String>();
if(digits.length() == 0) return ans;
helper(ans, digits, "", 0);
return ans;
}
public void helper(List<String> ans, String digits, String cur, int count){
if(count == digits.length()){
ans.add(cur);
return;
}
String temp = new String(cur);
for(char c : mapping[digits.charAt(count)-'0'].toCharArray()){
helper(ans, digits, cur+c, count+1);
cur = temp;
}
}
}
__________________________________________________________________________________________________