CC150 1.8

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 (i.e., “waterbottle” is a rotation of “erbottlewat”).


Trikky problem.

// Given method
boolean isSubstring(String target, String sub);

boolean isRotation(String s1, String s2)
{
  return isSubString(s1 + s1, s2);
}


你可能感兴趣的:(interview)