1. 程式人生 > >1.8-s2是否是s1的rotation(呼叫一次isSubstring)

1.8-s2是否是s1的rotation(呼叫一次isSubstring)

Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (i.e., “waterbottle” is a rotation of “erbottlewat”).

注意rotate是字串左移或者右移n位

解法很巧妙,拼接2個s1,判斷s2是否是s1s1的子串。

bool isRotation(string s1, string s2)
{
    if(s1.size()!=s2.size() || s1.size()==0 ||s2.size()==0)
        return false;
    string s1s1=s1+s1;
    if (isSubstring(s1s1, s2))
        return true;
    else
        return false;
}