String Rotation

1.9 String Rotation: Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, 51 and 52, write code to check if 52 is a rotation of 51 using only one call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat").

  • Brainstorm:

    • if we preform s1+s1 then the result is guaranteed to have the full word within it.

bool isRotation(string s1, string s2) {
  if (s1.length() > 0 && s2.length() > 0 && s1.length() == s2.length())  {
    return isSubstring(s2, s1+s1);
  }
  return false;

}

Last updated