package algorithmbasic.basicsets.class29;
public class RotationString {
public static Boolean isRotation(String str1, String str2) {
if (str1.length() != str2.length()) {
return false;
}
String str = str1 + str1;
return kmp(str, str2);
}
public static Boolean kmp(String str1, String str2) {
if (str1 == null || str2 == null || str1.length() < 1 || str2.length() < 1 || str2.length() > str1.length()) {
return false;
}
char[] strA = str1.toCharArray();
char[] strB = str2.toCharArray();
int x = 0;
int y = 0;
int[] NEXT = makeNextArray(strB);
while (x < strA.length && y < strB.length) {
if (strA[x] == strB[y]) {
x++;
y++;
} else if (y != 0) {
y = NEXT[y];
} else {
x++;
}
}
return y == strB.length ? true : false;
}
private static int[] makeNextArray(char[] strB) {
if (strB.length == 1) {
return new int[]{-1};
}
if (strB.length == 2) {
return new int[]{-1, 0};
}
int[] Next = new int[strB.length];
Next[0] = -1;
Next[1] = 0;
int c = 0;
for (int i = 2; i < strB.length; i++) {
if (strB[i - 1] == strB[c]) {
Next[i] = ++c;
} else if (c != 0) {
c = Next[c];
} else {
Next[i] = 0;
}
}
return Next;
}
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = "bacdef";
Boolean rotation = isRotation(str1, str2);
System.out.println(rotation);
}
}