1.8-s2是否是s1的rotation(调用一次isSubstring)

来源:互联网 发布:学生购买阿里云服务器 编辑:程序博客网 时间:2024/05/28 15:43

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

注意rotate是字符串左移或者右移n位

解法很巧妙,拼接2个s1,判断s2是否是s1s1的子串。

bool isRotation(string s1, string s2){    if(s1.size()!=s2.size() || s1.size()==0 ||s2.size()==0)        return false;    string s1s1=s1+s1;    if (isSubstring(s1s1, s2))        return true;    else        return false;}



0 0
原创粉丝点击