Problem
Check if a string is a rotation of another. For example:
Hello and loHel are a rotation pair.
Tony and Julia are not a rotation pair.
Solution
public class RotationOfString{
public static void main(String[] arguments){
RotationOfString.Test("Hello", "loHel");
RotationOfString.Test("Tony", "Julia");
}
public static void Test(String firstString, String secondString){
if(RotationOfString.IsRotated(firstString, secondString)){
System.out.println(firstString + " is a rotation of " + secondString + ".");
}else{
System.out.println(firstString + " isn't a rotation of " + secondString + ".");
}
}
public static boolean IsRotated(String firstString, String secondString){
StringBuffer secondStringBuffer = new StringBuffer(secondString);
secondStringBuffer.append(secondString);
return secondStringBuffer.indexOf(firstString)!=-1;
}
}
Output
Hello is a rotation of loHel.
Tony isn't a rotation of Julia.