public static int getIndexOf(String str1, String str2) {
if (str1 == null || str2 == null || str1.length() < str2.length()) {
return -1;
}
char[] mm = str1.toCharArray();
char[] nn = str2.toCharArray();
int[] next = getNextArray(str2);
int m = 0;
int n = 0;
while (m < mm.length && n < nn.length) {
if (mm[m] == nn[n]) {
m++;
n++;
} else if (n == 0) {
m++;
} else {
n = next[n];
}
}
return n == nn.length ? m-n : -1;
}
public static int[] getNextArray(String str) {
if (str.length() == 1) {
return new int[]{-1};
}
char[] str2 = str.toCharArray();
int[] next = new int[str.length()];
next[0] = -1;
next[1] = 0;
int i = 2;
int flag = 0;
while (i < str.length()) {
if (str2[i - 1] == str2[flag]) {
next[i] = flag + 1;
i++;
flag++;
} else if (flag > 0) {
flag = next[flag];
} else {
next[i] = 0;
i++;
}
}
return next;
}