题目描述
给定2个字符串s1和s2,请判断s2是否为s1旋转而成,返回bool值。字符串中字符为英文字母和空格,区分大小写,字符串长度小于等于1000。
测试样例:
"Hello world","worldhello "
返回:false
"waterbottle","erbottlewat"
返回:true
import java.util.*;
public class ReverseEqual {
public boolean checkReverseEqual(String s1, String s2) {
// write code here
boolean b = false;
if (s1 == s2)
b = true;
else {
for (int i = 1; i < s1.length(); i++) {
String temp = s1.substring(0, i);
String temp2 = s1.substring(i);
//System.out.println(i + " " + temp + " " + temp2);
if (((s2.contains(temp)) && (s2.contains(temp2)))
&& ((s2.charAt(s2.length() - 1) == temp.charAt(temp.length() - 1))
&& (s2.charAt(0) == temp2.charAt(0)))) {
b = true;
}
}
}
return b;
}
}