private static int[] createTable(String pattern) {
      int[] next = new int[pattern.length()];
      int j = -1;
      for (int i = 0; i < pattern.length(); i++) {
        if (i == 0)
          next[i] = -1;
        else if (pattern.charAt(i) != pattern.charAt(j))
          next[i] = j;
        else
          next[i] = next[j];
        while (j >= 0 && pattern.charAt(i) != pattern.charAt(j)) {
          j = next[j];
        }
        j++;
      }
      return next;
    }