Skip to content

Instantly share code, notes, and snippets.

@xun-gong
Created June 24, 2014 20:30
Show Gist options
  • Save xun-gong/7895504c829d08bba647 to your computer and use it in GitHub Desktop.
Save xun-gong/7895504c829d08bba647 to your computer and use it in GitHub Desktop.
CareerCup1.8.cpp
/* 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