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;
}
本文介绍了一种使用isSubstring方法检查一个字符串是否为另一个字符串的旋转版本的方法。通过将原字符串拼接自身并调用一次isSubstring方法,即可高效判断两字符串间的关系。
559

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



