How come in java when input is "" but my code returns [""] while in my own laptop it is [ ] which is the right answer?


  • 0
    F

    public class Solution {
    public List<String> result = new ArrayList<String>();
    public List<String> letterCombinations(String digits) {
    if(digits == null) return null;
    if(digits.length()==0)
    {
    result.add("");
    return result;
    }
    StringBuilder Scur = new StringBuilder();
    combination(digits,Scur);
    return result;
    }

    public void combination(String Scritic,StringBuilder Scur)
    {
        if(Scritic.length() ==1)
        {
            String s = getString(Scritic.charAt(0));
            for(int i=0;i<s.length();i++)
            {
                Scur.append(s.charAt(i));
                result.add(Scur.toString());
                Scur.delete(Scur.length()-1,Scur.length());
            }
        }
        else
        {
            String s = getString(Scritic.charAt(0));
            for(int i=0;i<s.length();i++)
            {
                Scur.append(s.charAt(i));
                combination(Scritic.substring(1),Scur);
                Scur.delete(Scur.length()-1,Scur.length());
            }
        }
    }
    
    public String getString(char c)
    {
        switch(c)
        {
            case '2':
                return "abc";
            case '3':
                return "def";
            case '4':
                return "ghi";
            case '5':
                return "jkl";
            case '6':
                return "mno";
            case '7':
                return "pqrs";
            case '8':
                return "tuv";
            case '9':
                return "wxyz";
            case '0':
                return " ";
            default :
                return "";
            
        }
    }
    

    }


  • 0
    A

    I have the same problem of you


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.