1. 程式人生 > >CareerCup 1.8

CareerCup 1.8

1.8 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 (e.g., "waterbottle" is a rotation of "erbottlewat").

bool isSubstring(string str, string substr) {
    return str.find(substr) != string::npos;
}

bool isRotation(string s1, string s2) {
    unsigned len = s1.length();
    if (s2.length() == len && len > 0) {
        return isSubstring(s1+s1, s2);
    }
    return false;
}