Skip to content

Commit

Permalink
added edited version of LetterCombinationsOfAPhoneNumber.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Naughton Jr authored and Kevin Naughton Jr committed May 29, 2018
1 parent a58514b commit 76c33ea
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 75 deletions.
42 changes: 27 additions & 15 deletions company/amazon/LetterCombinationsOfAPhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,41 @@
// Input:Digit string "23"
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

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

if(digits == null || digits.length() == 0) {
return result;
}

result.add("");
String[] mapping = {
"0",
"1",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};

for(int i = 0; i < digits.length(); i++) {
int current = Character.getNumericValue(digits.charAt(i));

while(result.peek().length() == i) {
String s = result.remove();

for(char c : mapping[current].toCharArray()) {
result.add(s + c);
}
}
}
letterCombinationsRecursive(result, digits, "", 0, mapping);

return result;
}

public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
if(index == digits.length()) {
result.add(current);
return;
}

String letters = mapping[digits.charAt(index) - '0'];
for(int i = 0; i < letters.length(); i++) {
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
}
}
}
42 changes: 27 additions & 15 deletions company/facebook/LetterCombinationsOfAPhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,41 @@
// Input:Digit string "23"
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

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

if(digits == null || digits.length() == 0) {
return result;
}

result.add("");
String[] mapping = {
"0",
"1",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};

for(int i = 0; i < digits.length(); i++) {
int current = Character.getNumericValue(digits.charAt(i));

while(result.peek().length() == i) {
String s = result.remove();

for(char c : mapping[current].toCharArray()) {
result.add(s + c);
}
}
}
letterCombinationsRecursive(result, digits, "", 0, mapping);

return result;
}

public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
if(index == digits.length()) {
result.add(current);
return;
}

String letters = mapping[digits.charAt(index) - '0'];
for(int i = 0; i < letters.length(); i++) {
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
}
}
}
42 changes: 27 additions & 15 deletions company/google/LetterCombinationsOfAPhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,41 @@
// Input:Digit string "23"
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

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

if(digits == null || digits.length() == 0) {
return result;
}

result.add("");
String[] mapping = {
"0",
"1",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};

for(int i = 0; i < digits.length(); i++) {
int current = Character.getNumericValue(digits.charAt(i));

while(result.peek().length() == i) {
String s = result.remove();

for(char c : mapping[current].toCharArray()) {
result.add(s + c);
}
}
}
letterCombinationsRecursive(result, digits, "", 0, mapping);

return result;
}

public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
if(index == digits.length()) {
result.add(current);
return;
}

String letters = mapping[digits.charAt(index) - '0'];
for(int i = 0; i < letters.length(); i++) {
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
}
}
}
42 changes: 27 additions & 15 deletions company/uber/LetterCombinationsOfAPhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,41 @@
// Input:Digit string "23"
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

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

if(digits == null || digits.length() == 0) {
return result;
}

result.add("");
String[] mapping = {
"0",
"1",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};

for(int i = 0; i < digits.length(); i++) {
int current = Character.getNumericValue(digits.charAt(i));

while(result.peek().length() == i) {
String s = result.remove();

for(char c : mapping[current].toCharArray()) {
result.add(s + c);
}
}
}
letterCombinationsRecursive(result, digits, "", 0, mapping);

return result;
}

public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
if(index == digits.length()) {
result.add(current);
return;
}

String letters = mapping[digits.charAt(index) - '0'];
for(int i = 0; i < letters.length(); i++) {
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
}
}
}
42 changes: 27 additions & 15 deletions leetcode/backtracking/LetterCombinationsOfAPhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,41 @@
// Input:Digit string "23"
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

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

if(digits == null || digits.length() == 0) {
return result;
}

result.add("");
String[] mapping = {
"0",
"1",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};

for(int i = 0; i < digits.length(); i++) {
int current = Character.getNumericValue(digits.charAt(i));

while(result.peek().length() == i) {
String s = result.remove();

for(char c : mapping[current].toCharArray()) {
result.add(s + c);
}
}
}
letterCombinationsRecursive(result, digits, "", 0, mapping);

return result;
}

public void letterCombinationsRecursive(List<String> result, String digits, String current, int index, String[] mapping) {
if(index == digits.length()) {
result.add(current);
return;
}

String letters = mapping[digits.charAt(index) - '0'];
for(int i = 0; i < letters.length(); i++) {
letterCombinationsRecursive(result, digits, current + letters.charAt(i), index + 1, mapping);
}
}
}

0 comments on commit 76c33ea

Please sign in to comment.