Problem
Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 (e.g.,“waterbottle” is a rotation of"erbottlewat"). Can you use only one call to the method that checks if one word is a substring of another?
Example1
Input: s1 = “waterbottle”, s2 = “erbottlewat”
Output: True
Example2
Input: s1 = “aa”, “aba”
Output: False
Solution
首先,长度不一样,肯定不是。
s1 = waterbottle,s2= erbottlewat。
另x = wat,y=erbottle,则s1 = xy,s2=yx。
如果将两个s2串接在一起,可以得到s=s2+s2 = yxyx,中间有xy=s1。
class Solution {
public:
bool isFlipedString(string s1, string s2) {
if(s1.length() != s2.length())
return false;
string s = s2 + s2;
return s.find(s1) != string::npos ;
}
};