MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / solve

Method solve

PrintNumPadPermutation.java:28–66  ·  view source on GitHub ↗
(HashMap<Character,String> map,String input,int n)

Source from the content-addressed store, hash-verified

26 }
27
28 public static ArrayList<String> solve(HashMap<Character,String> map,String input,int n)
29 {
30 // Base case is that if only 1 input is there then store it in list and return the list
31 if(n==1)
32 {
33
34 ArrayList<String> list = new ArrayList<>();
35 String res = map.get(input.charAt(n-1));
36
37 for(int i=0;i<res.length();i++)
38 list.add(Character.toString(res.charAt(i)));
39
40 return list;
41 }
42
43 // Get the previous result
44 ArrayList<String> l1 = solve(map,input,n-1);
45 // Get the cuurent result
46 ArrayList<String> current = new ArrayList<>();
47 ArrayList<String> r = new ArrayList<>();
48
49 String rest = map.get(input.charAt(n-1));
50 for(int i=0;i<rest.length();i++)
51 {
52 current.add(Character.toString(rest.charAt(i)));
53 }
54
55 // Combine the two list and store it in the final list
56 for(int i=0;i<l1.size();i++)
57 {
58 for(int j=0;j<current.size();j++)
59 {
60 String ni = l1.get(i)+current.get(j);
61 r.add(ni);
62 }
63 }
64 // return the resultant list
65 return r;
66 }
67}

Callers 1

mainMethod · 0.95

Calls 1

addMethod · 0.45

Tested by

no test coverage detected