Skip to content

Instantly share code, notes, and snippets.

@frank4565
Created February 13, 2014 05:33
Show Gist options
  • Save frank4565/8970264 to your computer and use it in GitHub Desktop.
Save frank4565/8970264 to your computer and use it in GitHub Desktop.
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 si using only one call to isSubstring (e.g.,"waterbottle"is a rota- tion of"erbottlewat").
#include<iostream>
using namespace std;
bool isSubstring(const string &s1, const string &s2)
{
return s1.find(s2) != string::npos;
}
bool isRotation(const string &s1, const string &s2)
{
if (s1.size() == s2.size() && s1.size() > 0) {
string s1s1 = s1 + s1;
return isSubstring(s1s1, s2);
}
return false;
}
int main(int argc, char *argv[])
{
cout << isRotation("waterbottle", "terbottlewa") << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment