Keypad Permutation

Problem

Phone has letters on the number keys. for example, number 2 has ABC on it, number 3 has DEF, 4 number has GHI,... , and number 9 has WXYZ. Write a program that will print out all of the possible combination of those letters depending on the input.

Solution


HashMap

 1 public static ArrayList<String> letterCombinations(String digits) {
 2   StringBuilder sb = new StringBuilder();
 3   ArrayList<String> res = new ArrayList<String>();
 4
 5     if(digits == null) {
 6         return res;
 7     }
 8
 9     //hashmap to store the key pad info
10     Map<Character, char[]> hm = new HashMap<Character, char[]>();
11     hm.put(‘2‘, new char[]{‘a‘, ‘b‘, ‘c‘});
12     hm.put(‘3‘, new char[]{‘d‘, ‘e‘, ‘f‘});
13     hm.put(‘4‘, new char[]{‘g‘, ‘h‘, ‘i‘});
14     hm.put(‘5‘, new char[]{‘j‘, ‘k‘, ‘l‘});
15     hm.put(‘6‘, new char[]{‘m‘, ‘n‘, ‘o‘});
16     hm.put(‘7‘, new char[]{‘p‘, ‘q‘, ‘r‘, ‘s‘});
17     hm.put(‘8‘, new char[]{‘t‘, ‘u‘, ‘v‘});
18     hm.put(‘9‘, new char[]{‘w‘, ‘x‘, ‘y‘, ‘z‘});
19
20     helper(digits, res, sb, hm, 0);
21
22     return res;
23
24 }
25
26 public static void helper(String digits, ArrayList<String> res, StringBuilder sb, Map<Character, char[]> hm, int pos) {
27   if(pos == digits.length()) {
28       res.add(sb.toString());
29       return;
30   }
31
32   for(int i=0; i<hm.get(digits.charAt(pos)).length; i++) {
33       sb.append(hm.get(digits.charAt(pos))[i]);
34       helper(digits, res, sb, hm, pos+1);
35       sb.deleteCharAt(sb.length()-1);
36   }
37 }
时间: 2024-10-08 21:41:56

Keypad Permutation的相关文章

Cipe Coding Summary Part1

1. Colorful Number:A numbercan be broken into different sub-sequence parts. Suppose a number 3245 can bebroken into parts like 3 2 4 5 32 24 45 324 245. And this number is a colorfulnumber, since product of every digit of a sub-sequence are different

31. Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme

Permutation Sequence

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3

Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme

LeetCode 31. Next Permutation

Problem: https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest poss

60. Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3

【数组】Next Permutation

题目: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repla

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

HDU3664 Permutation Counting

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 754 Problem Description Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-val