Home > Mobile >  Letter Combinations of a Phone Number Everything is correct except 1 test where input is just "
Letter Combinations of a Phone Number Everything is correct except 1 test where input is just "

Time:01-25

Failes test case SS

I have returned an empty String but the desired output shows I should return nothing. How can I return nothing? Pls, refer to the attached image, you'll understand the question.

Below I am adding my code pls tell me which part I can improve-

import java.util.*;
class Solution {
    public List<String> letterCombinations(String digits) {
        String mapping[]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        int num=0;
        try{
        num=Integer.parseInt(digits);
        } catch(NumberFormatException ex){ // handle your exception
            System.out.print("error");
        }
        List<String> ans=new ArrayList<>();
        // List<String> res=new ArrayList<>();
        ans = helper(num,mapping);
        return ans;
    }
    public List<String> helper(int num, String mapping[]){
        if(num<=0){
            List<String> ans=new ArrayList<>();
            ans.add("");
            return ans;
        }
        List<String> smallAns=new ArrayList<>();
        int myNum=num;
        smallAns=helper(num/10, mapping);
        String s=mapping[myNum];
        List<String> ans=new ArrayList<>();
        ans=appendStr(s, smallAns);
        return ans;
    }
    public List<String> appendStr(String s, List<String> res){
        List<String> out=new ArrayList<>();
        int l=s.length();
        int i=0;
        while(i<l){
            for(int j=0;j<res.size();j  ){
                String toAdd=res.get(j) s.charAt(i);
                out.add(toAdd);
            }
            i  ;
        }
        return out;
    }
}

CodePudding user response:

Added this block of code now it's working

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

The above block of code is added letterCombinations function which will return empty array list if the String provided by user is empty as required.

import java.util.*;
class Solution {
    public List<String> letterCombinations(String digits) {
        if(digits.length()==0){
            List<String> ans=new ArrayList<>();
            return ans;
        }
        String mapping[]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        int num=0;
        try{
        num=Integer.parseInt(digits);
        } catch(NumberFormatException ex){
            System.out.print("error");
        }
        List<String> ans=new ArrayList<>();
        ans = helper(num,mapping);
        return ans;
    }
    public List<String> helper(int num, String mapping[]){
        if(num<=0){
            List<String> ans=new ArrayList<>();
            ans.add("");
            return ans;
        }
        List<String> smallAns=new ArrayList<>();
        int myNum=num;
        smallAns=helper(num/10, mapping);
        String s=mapping[myNum];
        List<String> ans=new ArrayList<>();
        ans=appendStr(s, smallAns);
        return ans;
    }
    public List<String> appendStr(String s, List<String> res){
        List<String> out=new ArrayList<>();
        int l=s.length();
        int i=0;
        while(i<l){
            for(int j=0;j<res.size();j  ){
                String toAdd=res.get(j) s.charAt(i);
                out.add(toAdd);
            }
            i  ;
        }
        return out;
    }
}
  • Related