Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
Although the above answer is in lexicographical order, your answer could be in any order you want.
public class Solution { public void letterCombinationsAux(String digits, int i, char[][] dial, StringBuilder sb, Listres){ int len = digits.length(); if(i >= len){ res.add(sb.toString()); return; } int number = Character.getNumericValue(digits.charAt(i)); for(char c : dial[number]){ sb.append(c); letterCombinationsAux(digits, i+1, dial, sb, res); sb.deleteCharAt(sb.length()-1); } return; } public List letterCombinations(String digits) { char[][] dial = {{}, {}, {'a','b','c'},{'d','e','f'}, {'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}}; int len = digits.length(); ArrayList res = new ArrayList (); if(0 == len) return res; letterCombinationsAux(digits, 0, dial, new StringBuilder(), res); return res; } }
public class Solution { public void letterCombinationsAux(String digits, int i, char[][] dial, char[] path, Listres){ int len = digits.length(); if(i >= len){ res.add(new String(path)); return; } int number = Character.getNumericValue(digits.charAt(i)); for(char c : dial[number]){ path[i] = c; letterCombinationsAux(digits, i+1, dial, path, res); } return; } public List letterCombinations(String digits) { char[][] dial = {{}, {}, {'a','b','c'},{'d','e','f'}, {'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}}; int len = digits.length(); List res = new ArrayList (); if(0 == len) return res; letterCombinationsAux(digits, 0, dial, new char[len], res); return res; } }
No comments:
Post a Comment