## Saturday, June 4, 2016

### All combinations of strings that can be used to dial a number - GeeksforGeeks

All combinations of strings that can be used to dial a number - GeeksforGeeks
Given a number, print all possible combinations of strings that can be used to dial the given number in a phone with following specifications.
In the given phone, we can dial,
2 using A or B or C,
3 using D or E or F,
……………….
8 using T or U or V,
9 using W or X or Y or Z,
1 using only 1
0 using 0.
For example if 23, is the given phone number, the program should print AD, AE, AF, BD, BE, BF, CD, CE, CF

The idea is to store digit to characters mapping in hash map. The map stores all characters that can be used dial a digit. We place every possible character for current digit and recur for remaining digits
`    ``// A Recursive function to print all combinations`
`    ``// that can be used to dial a given number.`
`    ``// phNo ==> Given Phone Number`
`    ``// i    ==> Current digit of phNo to be processed`
`    ``// hm   ==> Stores characters that can be used to`
`    ``//          to dial a digit.`
`    ``// str  ==> Current output string`
`    ``static` `void` `printStrings(String phNo, ``int` `i,`
`                    ``HashMap<Character, String> hm,`
`                    ``StringBuilder str)`
`    ``{`
`        ``// If all digits are processed, print output`
`        ``// string`
`        ``if` `(i == phNo.length())`
`        ``{`
`            ``System.out.print(str + ``" "``);`
`            ``return``;`
`        ``}`

`        ``// Get current digit of phNo, and recur for all`
`        ``// characters that can be used to dial it.`
`        ``String s = hm.get(phNo.charAt(i));`
`        ``for` `(``int` `j = 0; j < s.length(); j++)`
`        ``{`
`            ``str.append(s.charAt(j));`
`            ``printStrings(phNo, i+1, hm, str);`
`            ``str.deleteCharAt(str.length()-1);`
`        ``}`
`    ``}`

`    ``// Prints all possible combinations of strings that`
`    ``// can be used to dial c[].`
`    ``static` `void` `printStringForNumber(String phNo)`
`    ``{`
`        ``// Create a HashMap`
`        ``HashMap<Character, String> hm =`
`                    ``new` `HashMap<Character, String>();`

`        ``// For every digit, store characters that can`
`        ``// be used to dial it.`
`        ``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"``);`
`        ``hm.put(``'1'``, ``"1"``);`
`        ``hm.put(``'0'``, ``"0"``);`

`        ``// Create a string to store a particular output`
`        ``// string`
`        ``StringBuilder str = ``new` `StringBuilder();`

`        ``// Call recursive function`
`        ``printStrings(phNo, 0, hm, str);`
`    ``}`
Read full article from All combinations of strings that can be used to dial a number - GeeksforGeeks