Skip to content

Commit 76c33ea

Browse files
Kevin Naughton JrKevin Naughton Jr
authored andcommitted
added edited version of LetterCombinationsOfAPhoneNumber.java
1 parent a58514b commit 76c33ea

5 files changed

Lines changed: 135 additions & 75 deletions

File tree

company/amazon/LetterCombinationsOfAPhoneNumber.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,41 @@
1414
// Input:Digit string "23"
1515
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
1616

17-
public class LetterCombinationsOfAPhoneNumber {
17+
class LetterCombinationsOfAPhoneNumber {
1818
public List<String> letterCombinations(String digits) {
19-
LinkedList<String> result = new LinkedList<>();
20-
String[] mapping = new String[] { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
19+
List<String> result = new ArrayList<String>();
2120

2221
if(digits == null || digits.length() == 0) {
2322
return result;
2423
}
2524

26-
result.add("");
25+
String[] mapping = {
26+
"0",
27+
"1",
28+
"abc",
29+
"def",
30+
"ghi",
31+
"jkl",
32+
"mno",
33+
"pqrs",
34+
"tuv",
35+
"wxyz"
36+
};
2737

28-
for(int i = 0; i < digits.length(); i++) {
29-
int current = Character.getNumericValue(digits.charAt(i));
30-
31-
while(result.peek().length() == i) {
32-
String s = result.remove();
33-
34-
for(char c : mapping[current].toCharArray()) {
35-
result.add(s + c);
36-
}
37-
}
38-
}
38+
letterCombinationsRecursive(result, digits, "", 0, mapping);
3939

4040
return result;
4141
}
42+
43+
public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
44+
if(index == digits.length()) {
45+
result.add(current);
46+
return;
47+
}
48+
49+
String letters = mapping[digits.charAt(index) - '0'];
50+
for(int i = 0; i < letters.length(); i++) {
51+
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
52+
}
53+
}
4254
}

company/facebook/LetterCombinationsOfAPhoneNumber.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,41 @@
1414
// Input:Digit string "23"
1515
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
1616

17-
public class LetterCombinationsOfAPhoneNumber {
17+
class LetterCombinationsOfAPhoneNumber {
1818
public List<String> letterCombinations(String digits) {
19-
LinkedList<String> result = new LinkedList<>();
20-
String[] mapping = new String[] { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
19+
List<String> result = new ArrayList<String>();
2120

2221
if(digits == null || digits.length() == 0) {
2322
return result;
2423
}
2524

26-
result.add("");
25+
String[] mapping = {
26+
"0",
27+
"1",
28+
"abc",
29+
"def",
30+
"ghi",
31+
"jkl",
32+
"mno",
33+
"pqrs",
34+
"tuv",
35+
"wxyz"
36+
};
2737

28-
for(int i = 0; i < digits.length(); i++) {
29-
int current = Character.getNumericValue(digits.charAt(i));
30-
31-
while(result.peek().length() == i) {
32-
String s = result.remove();
33-
34-
for(char c : mapping[current].toCharArray()) {
35-
result.add(s + c);
36-
}
37-
}
38-
}
38+
letterCombinationsRecursive(result, digits, "", 0, mapping);
3939

4040
return result;
4141
}
42+
43+
public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
44+
if(index == digits.length()) {
45+
result.add(current);
46+
return;
47+
}
48+
49+
String letters = mapping[digits.charAt(index) - '0'];
50+
for(int i = 0; i < letters.length(); i++) {
51+
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
52+
}
53+
}
4254
}

company/google/LetterCombinationsOfAPhoneNumber.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,41 @@
1414
// Input:Digit string "23"
1515
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
1616

17-
public class LetterCombinationOfAPhoneNumber {
17+
class LetterCombinationsOfAPhoneNumber {
1818
public List<String> letterCombinations(String digits) {
19-
LinkedList<String> result = new LinkedList<>();
20-
String[] mapping = new String[] { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
19+
List<String> result = new ArrayList<String>();
2120

2221
if(digits == null || digits.length() == 0) {
2322
return result;
2423
}
2524

26-
result.add("");
25+
String[] mapping = {
26+
"0",
27+
"1",
28+
"abc",
29+
"def",
30+
"ghi",
31+
"jkl",
32+
"mno",
33+
"pqrs",
34+
"tuv",
35+
"wxyz"
36+
};
2737

28-
for(int i = 0; i < digits.length(); i++) {
29-
int current = Character.getNumericValue(digits.charAt(i));
30-
31-
while(result.peek().length() == i) {
32-
String s = result.remove();
33-
34-
for(char c : mapping[current].toCharArray()) {
35-
result.add(s + c);
36-
}
37-
}
38-
}
38+
letterCombinationsRecursive(result, digits, "", 0, mapping);
3939

4040
return result;
4141
}
42+
43+
public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
44+
if(index == digits.length()) {
45+
result.add(current);
46+
return;
47+
}
48+
49+
String letters = mapping[digits.charAt(index) - '0'];
50+
for(int i = 0; i < letters.length(); i++) {
51+
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
52+
}
53+
}
4254
}

company/uber/LetterCombinationsOfAPhoneNumber.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,41 @@
1414
// Input:Digit string "23"
1515
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
1616

17-
public class LetterCombinationsOfAPhoneNumber {
17+
class LetterCombinationsOfAPhoneNumber {
1818
public List<String> letterCombinations(String digits) {
19-
LinkedList<String> result = new LinkedList<>();
20-
String[] mapping = new String[] { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
19+
List<String> result = new ArrayList<String>();
2120

2221
if(digits == null || digits.length() == 0) {
2322
return result;
2423
}
2524

26-
result.add("");
25+
String[] mapping = {
26+
"0",
27+
"1",
28+
"abc",
29+
"def",
30+
"ghi",
31+
"jkl",
32+
"mno",
33+
"pqrs",
34+
"tuv",
35+
"wxyz"
36+
};
2737

28-
for(int i = 0; i < digits.length(); i++) {
29-
int current = Character.getNumericValue(digits.charAt(i));
30-
31-
while(result.peek().length() == i) {
32-
String s = result.remove();
33-
34-
for(char c : mapping[current].toCharArray()) {
35-
result.add(s + c);
36-
}
37-
}
38-
}
38+
letterCombinationsRecursive(result, digits, "", 0, mapping);
3939

4040
return result;
4141
}
42+
43+
public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
44+
if(index == digits.length()) {
45+
result.add(current);
46+
return;
47+
}
48+
49+
String letters = mapping[digits.charAt(index) - '0'];
50+
for(int i = 0; i < letters.length(); i++) {
51+
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
52+
}
53+
}
4254
}

leetcode/backtracking/LetterCombinationsOfAPhoneNumber.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,41 @@
1414
// Input:Digit string "23"
1515
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
1616

17-
public class LetterCombinationsOfAPhoneNumber {
17+
class LetterCombinationsOfAPhoneNumber {
1818
public List<String> letterCombinations(String digits) {
19-
LinkedList<String> result = new LinkedList<>();
20-
String[] mapping = new String[] { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
19+
List<String> result = new ArrayList<String>();
2120

2221
if(digits == null || digits.length() == 0) {
2322
return result;
2423
}
2524

26-
result.add("");
25+
String[] mapping = {
26+
"0",
27+
"1",
28+
"abc",
29+
"def",
30+
"ghi",
31+
"jkl",
32+
"mno",
33+
"pqrs",
34+
"tuv",
35+
"wxyz"
36+
};
2737

28-
for(int i = 0; i < digits.length(); i++) {
29-
int current = Character.getNumericValue(digits.charAt(i));
30-
31-
while(result.peek().length() == i) {
32-
String s = result.remove();
33-
34-
for(char c : mapping[current].toCharArray()) {
35-
result.add(s + c);
36-
}
37-
}
38-
}
38+
letterCombinationsRecursive(result, digits, "", 0, mapping);
3939

4040
return result;
4141
}
42+
43+
public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
44+
if(index == digits.length()) {
45+
result.add(current);
46+
return;
47+
}
48+
49+
String letters = mapping[digits.charAt(index) - '0'];
50+
for(int i = 0; i < letters.length(); i++) {
51+
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
52+
}
53+
}
4254
}

0 commit comments

Comments
 (0)