Created
February 21, 2018 20:28
-
-
Save c0debrain/02b73040f5535ee204da63de0c6b251a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Created by shahin on 2/21/18. | |
*/ | |
class MapDigits { | |
//Input:Digit string "23" | |
//Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. | |
static Map<String, List<String>> digitMap = new HashMap<>(); | |
MapDigits() { | |
digitMap.put("2", Arrays.asList(new String[] {"a", "b", "c"})); | |
digitMap.put("3", Arrays.asList(new String[] {"d", "e", "f"})); | |
digitMap.put("4", Arrays.asList(new String[] {"g", "h", "i"})); | |
digitMap.put("5", Arrays.asList(new String[] {"j", "k", "l"})); | |
digitMap.put("6", Arrays.asList(new String[] {"m", "n", "o"})); | |
digitMap.put("7", Arrays.asList(new String[] {"p", "q", "r", "s"})); | |
digitMap.put("8", Arrays.asList(new String[] {"t", "u", "v"})); | |
digitMap.put("9", Arrays.asList(new String[] {"w", "x", "y", "z"})); | |
} | |
public List<String> letterCombinations(String digits) { | |
List<String> result = new ArrayList<>(); | |
/* | |
if(digits.length() == 0) { | |
System.out.println("doin for zero"); | |
return result; | |
} | |
*/ | |
if(digits.length() == 1) { | |
System.out.print("one: "); | |
List<String> one= digitMap.get(digits.substring(0, 1)); | |
System.out.println(one); | |
return one; | |
} | |
List<String> intermediate = letterCombinations(digits.substring(1, digits.length())); | |
//System.out.println(intermediate); | |
for(String first : digitMap.get(digits.substring(0,1))) | |
for(String rest : intermediate) | |
result.add(first + rest); | |
return result; | |
} | |
public static void main(String args[]) { | |
MapDigits md = new MapDigits(); | |
List<String> letters = md.letterCombinations("23"); | |
System.out.print("Anser: "); | |
System.out.println(letters); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment