Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Solution {
  5.   public static void main(String[] args) {
  6.     System.out.println("Running");
  7.    
  8.     Map<String, String> alphabet = new HashMap<String, String>();
  9.     alphabet.put("1", "a");
  10.     alphabet.put("2", "b");
  11.     alphabet.put("3", "c");
  12.     alphabet.put("4", "d");
  13.     alphabet.put("5", "e");
  14.     alphabet.put("6", "f");
  15.     alphabet.put("7", "g");
  16.     alphabet.put("8", "h");
  17.     alphabet.put("9", "i");
  18.     alphabet.put("10", "j");
  19.     alphabet.put("11", "k");
  20.     alphabet.put("12", "l");
  21.     alphabet.put("13", "m");
  22.     alphabet.put("14", "n");
  23.     alphabet.put("15", "o");
  24.     alphabet.put("16", "p");
  25.     alphabet.put("17", "q");
  26.     alphabet.put("18", "r");
  27.     alphabet.put("19", "s");
  28.     alphabet.put("20", "t");
  29.     alphabet.put("21", "u");
  30.     alphabet.put("22", "v");
  31.     alphabet.put("23", "w");
  32.     alphabet.put("24", "x");
  33.     alphabet.put("25", "y");
  34.     alphabet.put("26", "z");
  35.    
  36.     List<String> possibleStrings = getAllPossibleStrings(alphabet, "12345");
  37.     System.out.println("Possibilties");
  38.     if (possibleStrings == null) {
  39.       System.out.println("No valid strings which use the whole input");
  40.     } else {
  41.       for (String str : possibleStrings) {
  42.         System.out.println(" " + str);
  43.       }
  44.     }
  45.   }
  46.  
  47.   // Returns the list of all strings that could be made from input, encoded into the "alphabet" map.
  48.   // If there are no valid strings return null. Assumes "input" always starts with a value and that alphabet is initialized.
  49.   // If we can't assume that, I'd add a wrapper method which handles sanitation.
  50.   public static List<String> getAllPossibleStrings(Map<String, String> alphabet, String input) {
  51.     List<String> result = new ArrayList<String>();
  52.    
  53.     // Base case, we're at the end of the string.
  54.     if (input.length() == 0) {
  55.       result.add("");
  56.       return result;
  57.     }
  58.    
  59.     for (int i = 0; i < input.length(); i++) {
  60.       // Test each successive substring. If we cannot assume anything about the keys, then go until we're done.
  61.       // If we do know the range we can fast-fail and only check the number of digits in the dictionary possibilities.
  62.       String testKey = input.substring(0, i+1);
  63.      
  64.       if (alphabet.containsKey(testKey)) {
  65.         List<String> subProblemSolution = getAllPossibleStrings(alphabet, input.substring(i+1));
  66.        
  67.         // Sub-string isn't empty, but also has no valid options. In this case, the current substring
  68.         // is invalid, so check the next one.
  69.         if (subProblemSolution == null) {
  70.           continue;
  71.         }
  72.        
  73.         String testValue = alphabet.get(testKey);
  74.                
  75.         for (String soln : subProblemSolution) {
  76.           String newString = testValue + soln;
  77.           result.add(newString);
  78.         }
  79.       }
  80.     }
  81.    
  82.     if (result.size() == 0) {
  83.       return null;
  84.     }
  85.     return result;
  86.   }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement