给定两个字符串,A和B
A的旋转操作就是将A最左边的字符移动到最右边
例如abcde移动一次结果就是bcdea
经过若干次调整后,如果A能变成B,那么返回true
不能匹配成功,则返回false
java
package example;
public class demo05 {
public static void main(String[] args) {
String strA = "abcde";
String strB = "abdec";
//旋转字符串
int i;
for (i = 0; i < strA.length(); i++) {
strA = rotateStr(strA);
if (strA.equals(strB)) {
System.out.println("匹配成功");
break;
}
}
if (i == strA.length()) {
System.out.println("匹配失败");
}
}
public static String rotateStr(String str) {
StringBuilder newStr = new StringBuilder(str.length());
for (int i = 1; i < str.length(); i++) {
newStr.append(str.charAt(i));
}
newStr.append(str.charAt(0));
return newStr.toString();
}
}