796 Rotate String
We are given two strings,AandB.
A_shift onA_consists of taking stringAand moving the leftmost character to the rightmost position. For example, ifA = 'abcde', then it will be'bcdea'after one shift onA. ReturnTrueif and only ifAcan becomeBafter some number of shifts onA.
Example 1:
Input: A = 'abcde', B = 'cdeab'
Output: true
Example 2:
Input: A = 'abcde', B = 'abced'
Output: falseNote:
AandBwill have length at most100.
这题在cc189见过,如果是合法的rotattion的话,那个把原来的string连起来,那个rotate了的string会是那个的substring。例如:abcdeabcde里会有cdeab,如果不是合法的就不会是substring
public boolean rotateString(String A, String B) {
if (A == null || B == null) {
return true;
} else if (A.length() != B.length()) {
return false;
}
String total = A + A;
return total.indexOf(B) != -1;
}Last updated
Was this helpful?