Created
February 24, 2013 07:49
-
-
Save ThunderXu/5023045 to your computer and use it in GitHub Desktop.
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").
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
bool IsSubString(std::string, std::string); | |
bool IsRotation(std::string, std::string); | |
int main() | |
{ | |
using namespace std; | |
string str1,str2; | |
cin>>str1>>str2; | |
cout<<(IsRotation(str1,str2)?"Is Rotation":"Is not Rotation")<<endl; | |
return 0; | |
} | |
bool IsSubString(std::string str1, std::string str2) | |
{ | |
if(str1.find(str2)<str1.size()) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
bool IsRotation(std::string str1, std::string str2) | |
{ | |
if(str1.size()!=str2.size()) | |
{ | |
return false; | |
} | |
if(str1==str2) | |
{ | |
return true; | |
} | |
return IsSubString(str2.substr(1,str2.size())+str2.substr(0,str2.size()-1),str1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment