|
| 1 | +package backtracking; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +// https://leetcode.com/problems/letter-combinations-of-a-phone-number/ |
| 6 | +public class LetterCombinationsOfAPhoneNumber { |
| 7 | + |
| 8 | + Map<Character, String> phone = new HashMap<Character, String>() {{ |
| 9 | + put('2', "abc"); |
| 10 | + put('3', "def"); |
| 11 | + put('4', "ghi"); |
| 12 | + put('5', "jkl"); |
| 13 | + put('6', "mno"); |
| 14 | + put('7', "pqrs"); |
| 15 | + put('8', "tuv"); |
| 16 | + put('9', "wxyz"); |
| 17 | + }}; |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + LetterCombinationsOfAPhoneNumber obj = new LetterCombinationsOfAPhoneNumber(); |
| 21 | + obj.letterCombinationWithIterate("23"); |
| 22 | + } |
| 23 | + |
| 24 | + public List<String> letterCombinationWithIterate(String digits) { |
| 25 | + List<String> result = new ArrayList<>(); |
| 26 | + if (digits == null || digits.length() == 0) { |
| 27 | + return result; |
| 28 | + } |
| 29 | + |
| 30 | + char[][] map = new char[8][]; |
| 31 | + map[0] = "abc".toCharArray(); |
| 32 | + map[1] = "def".toCharArray(); |
| 33 | + map[2] = "ghi".toCharArray(); |
| 34 | + map[3] = "jkl".toCharArray(); |
| 35 | + map[4] = "mno".toCharArray(); |
| 36 | + map[5] = "pqrs".toCharArray(); |
| 37 | + map[6] = "tuv".toCharArray(); |
| 38 | + map[7] = "wxyz".toCharArray(); |
| 39 | + |
| 40 | + result.add(""); |
| 41 | + for (char digit: digits.toCharArray()) { |
| 42 | + result = append(result, map[digit - '2']); |
| 43 | + } |
| 44 | + |
| 45 | + System.out.println("result > " + Arrays.toString(result.toArray())); |
| 46 | + |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + private List<String> append(List<String> lastList, char[] charArray) { |
| 51 | + List<String> nextList = new ArrayList<>(); |
| 52 | + for (String s: lastList) { |
| 53 | + for (char c: charArray) { |
| 54 | + nextList.add(s + c); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return nextList; |
| 59 | + } |
| 60 | + |
| 61 | + private List<String> resultList = new ArrayList<>(); |
| 62 | + |
| 63 | + public List<String> letterCombinationsWithBacktrack(String digits) { |
| 64 | + if (digits == null || digits.length() == 0) { |
| 65 | + return resultList; |
| 66 | + } |
| 67 | + backTrack("", digits); |
| 68 | + |
| 69 | + return resultList; |
| 70 | + } |
| 71 | + |
| 72 | + public void backTrack(String combination, String nextDigits) { |
| 73 | + if (nextDigits == null || nextDigits.length() == 0) { |
| 74 | + resultList.add(combination); |
| 75 | + return; |
| 76 | + } |
| 77 | + char c = nextDigits.charAt(0); |
| 78 | + for (char item: phone.get(c).toCharArray()) { |
| 79 | + backTrack(combination + item, nextDigits.substring(1)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + public List<String> letterCombinations(String digits) { |
| 85 | + // result queue |
| 86 | + List<String> queue = new ArrayList<>(); |
| 87 | + if (digits == null || digits.length() == 0) { |
| 88 | + return queue; |
| 89 | + } |
| 90 | + // first |
| 91 | + queue.add(""); |
| 92 | + int gap; |
| 93 | + int size; |
| 94 | + String charString; |
| 95 | + for (char c: digits.toCharArray()) { |
| 96 | + charString = phone.get(c); |
| 97 | + size = queue.size(); |
| 98 | + // iterate exist queue |
| 99 | + for (int i = 0; i < size; i++) { |
| 100 | + String s = queue.remove(0); |
| 101 | + // append char |
| 102 | + for (char phoneChar: charString.toCharArray()) { |
| 103 | + String result = s + phoneChar; |
| 104 | + queue.add(result); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return queue; |
| 110 | + } |
| 111 | +} |
0 commit comments