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”).
// Nothing difficult on this problem.
// supposed the input is ""waterbottle", its rotation is "erbottlewat"
// example: rotation + rotation = erbottles"waterbottle"wat.
bool checkRotation(string s1, string s2) {
string s3 = s2 + s2;
return isSubstring(s1, s3);
}
本文介绍了一种通过字符串拼接来检查一个字符串是否是另一个字符串的旋转的方法,仅需一次调用isSubstring函数。
226

被折叠的 条评论
为什么被折叠?



