数组与字符串 1.8

假定有一个方法isSubstring,可检查一个单词是否为其他字符串的字串。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成,要求只能调用一次isSubstring。(比如,waterbottle是erbottlewat旋转后的字符串。)

分析:将两个s1拼接起来,调用isSubstring确定s2是否为s1s1的子串。

1 bool check( string s1, string s2 ) {
2     if( s1.length() != s2.length() ) { return false; }
3     s1 += s1;
4     return isSubstring( s1, s2 );
5 }