We are given two strings, A
and B
.
A shift on A
consists of taking string A
and moving the leftmost character to the rightmost position. For example, if A = 'abcde'
, then it will be 'bcdea'
after one shift on A
. Return True
if and only if A
can become B
after some number of shifts on A
.
方法一:
循环将B左移一位,然后用A.equals(B),相等就使flag=true。退出循环,返回
class Solution {
public boolean rotateString(String A, String B) {
boolean flag = false;
for(int i = 0 ; i < A.length() ; i++){
B = B.substring(1) + B.charAt(0);
if(A.equals(B)){
flag = true;
break;
}
}
return flag;
}
}
方法二:
复制A加A的后面再赋给A,用contains()判断是否存在B这样的子串
class Solution {
public boolean rotateString(String A, String B) {
String C = A + A;
return C.contains(B);
}
}