您的位置:首页 > 其它

翻转字符串

2013-05-15 09:17 141 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  字符串 算法