Created
June 24, 2014 20:30
-
-
Save xun-gong/7895504c829d08bba647 to your computer and use it in GitHub Desktop.
CareerCup1.8.cpp
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
/* Chapter 1 | |
* 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") | |
*/ | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
bool isSubstring(string s1, string s2); // Return true if s2 is substring of s1 | |
bool isRotation(string s1, string s2) { | |
// First, the length must be the same | |
// Second, the rotation cut point must make s1 into xy | |
// s2 = yx (rotation). Check if s2 is substring of s1s1: | |
// yx isSub of xyxy? if yes, return true;else return false. | |
if (s1.size() == s2.size() && isSubstring(s1 + s1, s2) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
// Main Function | |
int main(int argc, char const *argv[]) | |
{ | |
string s1 = "waterbottle"; | |
string s2 = "erbottlewat"; | |
isRotation(s1, s2) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment