import java.util.regex.Pattern;

class Test {
  private Pattern pattern;
  private boolean[] table;

  public boolean matches1(String a) {
    return pattern.matcher(a).matches();
  }

  public boolean matches2(String a) {
    int length = a.length();
    for (int i = 0; i < length; ++i)
      if (!this.table[a.charAt(i)]) return false;
    return true;
  }

  public Test() {
    String chars = "adhst32pomnbqf51kczyx94";
    this.pattern = Pattern.compile(String.format("[%s]*", chars));
    this.table = new boolean[256];
    for (int i = 0; i < chars.length(); ++i)
      this.table[chars.charAt(i)] = true;
  }

  public static void main(String[] args) {
    boolean matches;
    Test test = new Test();

    long start;

    System.out.println("Regex\n=========================");
    start = System.currentTimeMillis();
    // best case
    for (int i = 0; i < 100000; ++i)
      if (test.matches1("egijlruvw0678"))
        System.out.println("error!");

    System.out.printf("Time 1: %d\n", (System.currentTimeMillis() - start));
    start = System.currentTimeMillis();

    // worst case
    for (int i = 0; i < 1000000; ++i)
      if (!test.matches1("st32pomnbqf51kczyx9"))
        System.out.println("error!");

    System.out.printf("Time 2: %d\n", (System.currentTimeMillis() - start));
    start = System.currentTimeMillis();

    // average case
    for (int i = 0; i < 1000000; ++i)
      if (!test.matches1("st32pomnbq2"))
        System.out.println("error!");

    System.out.printf("Time 3: %d\n", (System.currentTimeMillis() - start));

    System.out.println("Lookup table\n=========================");
    start = System.currentTimeMillis();
    // best case
    for (int i = 0; i < 100000; ++i)
      if (test.matches2("egijlruvw0678"))
        System.out.println("error!");

    System.out.printf("Time 1: %d\n", (System.currentTimeMillis() - start));
    start = System.currentTimeMillis();

    // worst case
    for (int i = 0; i < 1000000; ++i)
      if (!test.matches2("st32pomnbqf51kczyx9"))
        System.out.println("error!");

    System.out.printf("Time 2: %d\n", (System.currentTimeMillis() - start));
    start = System.currentTimeMillis();

    // average case
    for (int i = 0; i < 1000000; ++i)
      if (!test.matches2("st32pomnbq2"))
        System.out.println("error!");

    System.out.printf("Time 3: %d\n", (System.currentTimeMillis() - start));
  }
}
