'How to solve this leetcode problem of phone number pattern

package Code;
import java.util.*;

public class PhoneNumber {
    public static void main(String[] args) {
        int[] digits = { 2, 3 };
        // int n = number.length;

        // Function call
        letterCombinations(digits);
    }

    private static List<String> letterCombinations(int[] digits) {
        List<String> ans = new ArrayList<>();
        if(digits.length==0)
            return ans;

        HashMap<Character, String> hm = new HashMap<>();
        hm.put('2', "abc");
        hm.put('3', "def");
        hm.put('4', "ghi");
        hm.put('5', "jkl");
        hm.put('6', "mno");
        hm.put('7', "pqrs");
        hm.put('8', "tuv");
        hm.put('9', "wxyz");

        //backtrack(digits, 0, hm, new StringBuilder(), ans);
        backtrack(digits,0,hm,new StringBuilder(),ans);
        return ans;
    }

    private static void backtrack(String digits, int i, HashMap<Character, String> hm, StringBuilder sb, List<String> ans){
        if(i == digits.length()){
            ans.add(sb.toString());
            return;
        }
        String curr =  hm.get(digits.charAt(i));
        for(int k =0; k <curr.length(); k++){
            sb.append(curr.charAt(k));
            backtrack(digits, i+1, hm,  sb, ans);
            sb.deleteCharAt(sb.length() -1);
        }
    }



}

I have added the code but in backtrack(digits,0,hm,new StringBuilder(),ans) this line digits I am getting a red error. Can anyone please help me. It seems some int nad string mismatch but I am not sure what change should I do in the code.java



Solution 1:[1]

In letterCombinations(int[] digits) method, the param is int[], but in backtrack(String digits, ...), the param is String, maybe you can change then to String. Just like this

    public static void main(String[] args) {
        String digits = "23";
        // int n = number.length;

        // Function call
        List<String> strings = letterCombinations(digits);
        for (String string : strings) {
            System.out.println(string);
        }
    }

    private static List<String> letterCombinations(String digits) {
        List<String> ans = new ArrayList<>();
        if(digits.length()==0)
            return ans;

        HashMap<Character, String> hm = new HashMap<>();
        hm.put('2', "abc");
        hm.put('3', "def");
        hm.put('4', "ghi");
        hm.put('5', "jkl");
        hm.put('6', "mno");
        hm.put('7', "pqrs");
        hm.put('8', "tuv");
        hm.put('9', "wxyz");

        //backtrack(digits, 0, hm, new StringBuilder(), ans);
        backtrack(digits,0,hm,new StringBuilder(),ans);
        return ans;
    }

    private static void backtrack(String digits, int i, HashMap<Character, String> hm, StringBuilder sb, List<String> ans){
        if(i == digits.length()){
            ans.add(sb.toString());
            return;
        }
        String curr =  hm.get(digits.charAt(i));
        for(int k =0; k <curr.length(); k++){
            sb.append(curr.charAt(k));
            backtrack(digits, i+1, hm,  sb, ans);
            sb.deleteCharAt(sb.length() -1);
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cayman98