翻转字符串
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.
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.
相关文章推荐
- 翻转单词+左旋字符串
- PHP 中文字符串截取和翻转函数
- JavaScript中prototype(原型)给字符串对象添加一个toCharArray的方法,reverse(翻转)的 方法
- 关于字符串翻转的一些事
- python字符串翻转
- LeetCode 541. Reverse String II (字符串翻转)
- 翻转(reverse)字符串(string)的所有方法 详解
- 翻转字符串
- js字符串翻转方法大全?
- 翻转字符串中的单词
- 翻转字符串和比较两个字符串是否相同
- 翻转单词顺序与左旋转字符串
- [Lintcode] #53 翻转字符串
- 很实用的字符串翻转(拿过来就可以用的)
- 翻转单词顺序vs左旋转字符串
- [LintCode]53.翻转字符串
- 面试题 字符串翻转
- LintCode_053_翻转字符串
- 翻转(reverse)字符串(string)的所有方法
- 去除字符串中的空格,并将字符串翻转