-2

Given an integer n, using phone keypad find out all the possible strings that can be made using digits of input n. Return empty string for numbers 0 and 1.

Input

23

Output

ad
ae
af
bd
be
bf
cd
ce
cf

Code:

public class returnKeypad {
    private static String[] correspondingkepad(int n) {
        String keyWords[];

        switch (n) {
            case 2:  
                keyWords = new String[]{"a","b","c"};
                break;
            case 3:  
                keyWords = new String[]{"d","e","f"};
                break;
            case 4: 
                keyWords = new String[]{"g","h","i"};
                break;
            case 5:  
                keyWords = new String[]{"j","k","l"};
                break;
            case 6:
                keyWords = new String[]{"m","n","o"};
                break;
            case 7: 
                keyWords = new String[]{"p","q","r","s"};
                break;
            case 8: 
                keyWords = new String[]{"t","u","v"};
                break;
            case 9:  
                keyWords = new String[]{"w","x","y","z"};
                break;
           default:  
               keyWords = new String[]{""}; 
        }

        return keyWords;
    }

    public static String[] keypad(int n){
        if (n == 0) {
            String[] ans = {""};
            return ans;
        }

        String[] smallAns = keypad( n / 10 );      //line 45

        String[] p = correspondingkepad(n % 10);

        String[] ans = new String[p.length * smallAns.length];

        int k = 0;

        for(int i = 0; i < ans.length; i++) {
            for(int j = 0; j < p.length; j++) {
                ans[k] =  smallAns[i] + p[j];  //line 55             
                k++;
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        String s = "xyz";
        String[] ans = keypad(234); //line 66

        for(int i = 0; i < ans.length; i++) {
            System.out.println(ans[i]);
        }
    }
}

Exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 
    at returnKeypad.keypad(returnKeypad.java:55)
    at returnKeypad.keypad(returnKeypad.java:45)
    at returnKeypad.keypad(returnKeypad.java:45)
    at returnKeypad.main(returnKeypad.java:66

I am mentioning lines Number in comments

Please tell me if i am wrong with this approach to the problem or what is wrong in my code Thank you

Jacob G.
  • 28,856
  • 5
  • 62
  • 116

2 Answers2

2

you iterate on 'i' in the size of ans rather than smallAns

Change:

for(int i = 0; i<ans.length; i++) {
         for(int j =0; j<p.length; j++) {
             ans[k] =  smallAns[i] + p[j];  //Line55             
             k++;
         }
     }

To:

for(int i = 0; i<smallAns.length; i++) {
         for(int j =0; j<p.length; j++) {
             ans[k] =  smallAns[i] + p[j];  //Line55             
             k++;
         }
 }

That will fix the out-of-bound error, I'm not sure the rest of the logic makes sense...

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
1

Your smallAns[i] is not having any value other than ""and hence when i is getting increased to 1 it's failing with java.lang.ArrayIndexOutOfBoundsException. Please refine your logic in keypad(int n); Since you are using recursive call, on return it will come out of only last iteration.

Sagar Kharab
  • 369
  • 2
  • 18