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
.
Example 1: Input: A = 'abcde', B = 'cdeab' Output: true Example 2: Input: A = 'abcde', B = 'abced' Output: false
Note:
A
andB
will have length at most100
.
对A字符串进行移位,将最左字符移到最右,判断能否通过进行若干次移位操作,得到B字符串
思路:将已经移到最右的字符重接到左边,若能得到A字符串,则返回真;在这类并不需要知道知道移位进行了多少次,将两个B字符串直接首尾相接,若B字符串由A字符串移位而来,那么两个B字符串首尾相接一定包含A字符串,代码如下:
class Solution {
public boolean rotateString(String A, String B) {
return A.length() == B.length() && (A + A).indexOf(B) != -1;
}
}