Skip to content

Instantly share code, notes, and snippets.

@jason51122
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason51122/6f636b270039087a41ce to your computer and use it in GitHub Desktop.
Save jason51122/6f636b270039087a41ce to your computer and use it in GitHub Desktop.
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").
boolean checkRotation(String str1, String str2) {
if (str1 == null || str2 == null || str1.length() != str2.length()) {
return false;
}
String str = str1 + str1;
if (str.indexOf(str2) != -1) {
return true;
}
else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment