Skip to content

Instantly share code, notes, and snippets.

@zac-xin
Created April 23, 2012 13:14
Show Gist options
  • Save zac-xin/2470846 to your computer and use it in GitHub Desktop.
Save zac-xin/2470846 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 (i e , “waterbottle” is a rotation of “erbottlewat”)
*/
public class SubString {
public static void main(String args[]){
String s1 = "waterbottle";
String s2 = "erbottlewat";
System.out.println(isSubString(s1, s2));
//把两个s1连接起来
System.out.println(isSubString(s1 + s1, s2));
}
public static boolean isSubString(String s1, String s2){
int i;
i = s1.indexOf(s2);
return i >= 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment