String [] s2 is determined whether the rotation of the string s1

题目
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”).

Suppose you have a isSubstring function, it can detect whether a string is a substring of another string. S1 and s2 given string, can be used only once isSubstring rotation is determined whether the string s1 s2 Please write code. 
For example: "waterbottle" is "erbottlewat" string rotation.
 

Ideas:

Get only one point: s1 + s1 contains all the strings s1 rotation, it is equivalent to determining whether the substring s2 s1, s1 + .

//判断s2是不是s1的子串
bool isSubstring(string s1, string s2){
    if(s1.find(s2) != string::npos) return true;
    else return false;
}

//防范一下,以及调用:isSubstring(s1+s1, s2)
bool isRotation(string s1, string s2){
    if(s1.length() != s2.length() || s1.length()<=0)
        return false;
    return isSubstring(s1+s1, s2);
}

 

such as:

s1 = waterbottle, a rotating string is erbottlewat

s1 + s1 = waterbottlewaterbottle

Guess you like

Origin blog.csdn.net/m0_38033475/article/details/92380657