Phone Button Problem(电话按键问题)

Posted Dream_it_possible!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Phone Button Problem(电话按键问题)相关的知识,希望对你有一定的参考价值。

Question

        Given a string containing digits from 2-9 inclusive. please return all possible letter combinations that the number could represent.

example 

Input

23

Output

["ad","ae","af","bd","be","bf","cd","ce","cf"] 

Thinking

        This problem is similar to permutation and combination.We can image that the problem actually is combining two different string as an array. So recursion can reduce the dimension of the problem, it makes the previous result as the next condition until the final result returned.

        

        First,  we should get an array from st0 ,st1 as a condition of recurision, please see the following code:

    private static String[] findAllPossibleCombinations(String resource) {
        if (resource.length() <= 1) {
            return new String[0];
        }
        String st0 = mappingString(resource.charAt(0) + "");
        String st1 = mappingString(resource.charAt(1) + "");
        String[] str = new String[st0.length() * st1.length()];
        int index = 0;
        for (int i = 0; i < st0.length(); i++) {
            for (int j = 0; j < st1.length(); j++) {
                str[index] = st0.charAt(i) + "" + st1.charAt(j);
                index++;
            }
        }
        return combine(str, resource.substring(2));
    }

        then write the way of combine, and how to preform recursion?  we can constantly split the length of 'resource', in addition,  we should allocate an array of s1 and s2 product.

    private static String[] combine(String[] s1, String resource) {
        if (resource.length() == 0) {
            return s1;
        }
        String s2 = mappingString(resource.charAt(resource.length() - 1) + "");
        String[] result = new String[s1.length * s2.length()];
        int index = 0;
        for (int i = 0; i < s1.length; i++) {
            for (int j = 0; j < s2.length(); j++) {
                result[index] = s1[i] + "" + s2.charAt(j);
                index++;
            }
        }
        return combine(result, resource.substring(0, resource.length() - 1));
    }

 tips:  the condition of ending recursion is when resouce.length==0;

Test

Input

"234"

Output

 Input:

"23479"

Output: 

Complete Code

package leetcode100;

import java.util.Arrays;

/**
 * @author bingbing
 * @date 2021/8/18 0018 15:06
 * Phone Button Problem
 */
public class PhoneButtonProblem {


    public static void main(String[] args) {
        String resource = "239";
        String[] result = findAllPossibleCombinations(resource);
        System.out.println("totals are :" + result.length + ", all possible combinations are :" + Arrays.toString(result));
    }

    /**
     * @param resource
     * @return
     */
    private static String[] findAllPossibleCombinations(String resource) {
        if (resource.length() <= 1) {
            return new String[0];
        }
        String st0 = mappingString(resource.charAt(0) + "");
        String st1 = mappingString(resource.charAt(1) + "");
        String[] str = new String[st0.length() * st1.length()];
        int index = 0;
        for (int i = 0; i < st0.length(); i++) {
            for (int j = 0; j < st1.length(); j++) {
                str[index] = st0.charAt(i) + "" + st1.charAt(j);
                index++;
            }
        }
        return combine(str, resource.substring(2));
    }

    /**
     * recursive
     *
     * @param s1
     * @param resource
     * @return
     */
    private static String[] combine(String[] s1, String resource) {
        if (resource.length() == 0) {
            return s1;
        }
        String s2 = mappingString(resource.charAt(resource.length() - 1) + "");
        String[] result = new String[s1.length * s2.length()];
        int index = 0;
        for (int i = 0; i < s1.length; i++) {
            for (int j = 0; j < s2.length(); j++) {
                result[index] = s1[i] + "" + s2.charAt(j);
                index++;
            }
        }
        return combine(result, resource.substring(0, resource.length() - 1));
    }

    /**
     * mapping number to string
     *
     * @param str
     * @return
     */
    private static String mappingString(String str) {
        String target = "";
        switch (str) {
            case "2":
                target = "abc";
                break;
            case "3":
                target = "def";
                break;
            case "4":
                target = "ghi";
                break;
            case "5":
                target = "jkl";
                break;
            case "6":
                target = "mno";
                break;
            case "7":
                target = "pqrs";
                break;
            case "8":
                target = "tuv";
                break;
            case "9":
                target = "wxyz";
                break;
            default:
                break;
        }
        if ("".equals(target)) {
            throw new IllegalArgumentException("illegal param!");
        }
        return target;
    }


}

以上是关于Phone Button Problem(电话按键问题)的主要内容,如果未能解决你的问题,请参考以下文章

从嵌套字典中按键提取值

LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)

Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

[LeetCode in Python] 17 (M) letter combinations of a phone number 电话号码的字母组合

[POJ 3630] Phone List

leetcode-电话号码的字母组合