// Collection of code snippets by Arne VajhÃ¸j 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.Arrays;
import java.util.HashMap;

public class NameLookup {
   private HashMap data = null;
   public NameLookup() {
      data = new HashMap();
      String[] as = { "Arne", "Anders", "Asger", "Allan" };
      String[] bs = { "Børge", "Benny", "Brian", "Billy", "Bastian", "Bo", "Benjamin", "Be" };
      String[] cs = { };
      Arrays.sort(as);
      Arrays.sort(bs);
      Arrays.sort(cs);
      data.put("A", as);
      data.put("B", bs);
      data.put("C", cs);
   }
   public String matches(String s) {
      if(s.length() < 1) {
         return "";
      }
      String[] a = (String[])data.get(s.substring(0,1));
      if(a == null) {
         return "";
      }
      if(a.length <= 0) {
         return "";
      }
      StringBuffer sb = new StringBuffer("");
      int l = 0;
      int lmax = a.length - 1;
      while(lmax > l) {
         int med = (l + lmax) / 2;
         int cmp = s.compareTo(a[med]);
         if(cmp <= 0) {
            lmax = med;
         } else if(cmp > 0) {
            l = med + 1;
         }
      }
      if(a[l].indexOf(s) != 0) {
         return "";
      }
      int r = l;
      while((r < a.length) && (a[r].indexOf(s) == 0)) {
         r++;
      }
      for(int i = l; i < r; i++) {
         if(i > l) {
            sb.append(" ");
         }
         sb.append(a[i]);
      }
      return sb.toString();
   }
   private static NameLookup wl;
   public static void main(String[] args) {
      wl = new NameLookup();
      test("As");
      test("Ax");
      test("Be");
      test("Ba");
      test("Bø");
      test("B");
      test("Ca");
      test("De");
   }
   public static void test(String s) {
      System.out.println(s + " : " + wl.matches(s));
   }
}
