Last active
December 10, 2015 01:19
-
-
Save zac-xin/4357320 to your computer and use it in GitHub Desktop.
Given a digit string, return all possible letter combinations that the number could represent.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public ArrayList<String> letterCombinations(String digits) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
ArrayList<String> result = new ArrayList<String>(); | |
char str[] = new char[digits.length()]; | |
helper(0, str, result, digits); | |
return result; | |
} | |
public void helper(int level, char[] str, ArrayList<String> result, String digits){ | |
if(level == digits.length()){ | |
result.add(new String(str)); | |
return; | |
} | |
int current = digits.charAt(level) - '0'; | |
char[] record = getX(current).toCharArray(); | |
for(int i = 0 ; i < record.length; i++){ | |
str[level] = record[i]; | |
helper(level + 1, str, result, digits); | |
} | |
} | |
public String getX(int i){ | |
String[] arr = {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; | |
return arr[i]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment