1、常规思路:遍历字符串A,从当前位置把A分割head和tail两部分字符串,然后按照tail+head拼接字符串,比较其与B是否相等,相等就返回true,如果遍历完没有返回true,就返回false
import java.util.*;
public class Solution {
/**
* 旋转字符串
* @param A string字符串
* @param B string字符串
* @return bool布尔型
*/
public boolean solve (String A, String B) {
// write code here
if(A.equals(B)) {
return true;
}
for(int i = 1; i < A.length();i++) {
String str1 = A.substring(0,i);
String str2 = A.substring(i);
String str3 = str2 + str1;
if(str3.equals(B)) {
return true;
}
}
return false;
}
}
2、如果B能通过A旋转得到,那么B一定是A+A的子串
import java.util.*;
public class Solution {
/**
* 旋转字符串
* @param A string字符串
* @param B string字符串
* @return bool布尔型
*/
public boolean solve (String A, String B) {
// write code here
if(A.length() != B.length()) {
return false;
}
if(A.equals(B)) {
return true;
}
A = A + A;
return A.contains(B);
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容