Java 编写代码,仅使用一次对isSubstring的调用来检查s2是否是s1的旋转
假设您有一个方法isSubstring,它检查一个单词是否是另一个单词的子字符串。给定两个字符串s1和s2,编写代码检查s2是否是s1的旋转,只需调用一次isSubstring (例如,“waterbottle”是“erbottlewat”的轮换) 下面是我的代码:Java 编写代码,仅使用一次对isSubstring的调用来检查s2是否是s1的旋转,java,string,algorithm,stringbuffer,Java,String,Algorithm,Stringbuffer,假设您有一个方法isSubstring,它检查一个单词是否是另一个单词的子字符串。给定两个字符串s1和s2,编写代码检查s2是否是s1的旋转,只需调用一次isSubstring (例如,“waterbottle”是“erbottlewat”的轮换) 下面是我的代码: public class IsRotation1_8 { /* * Idea: Same idea as the code in CC150 book * First, if the s2 is a r
public class IsRotation1_8 {
/*
* Idea: Same idea as the code in CC150 book
* First, if the s2 is a rotation of s1, they should have the same length
* Then, check whether the s2 is a subString of s1 + s1
*
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
private static boolean isRotation(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
String s3 = s1 + s1;
// return isSubString(s3, s2);
return isSubString2(s3, s2);
}
private static boolean isSubString(String s3, String s2) {
return s3.contains(s2);
// return s3.indexOf(s2)!=-1;
}
/*
* Implement the isSubString method without using the method of Contains and indexOf
*/
private static boolean isSubString2(String s3, String s2){
int lengthS3 = s3.length();
int lengthS2 = s2.length();
for (int i = 0; i < lengthS3; i++) {
int j = i;
int k = 0;
while(s2.charAt(k++) == s3.charAt(j++)){
if(k == lengthS2)
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter string1 (q to quit):");
while (input.hasNextLine()) {
String string1 = input.nextLine();
if (string1.equals("q")) {
System.out.println("quit...");
System.exit(0);
}
System.out.println("Please enter string2:");
String string2 = input.nextLine();
System.out.println("Is s2 a rotation of s1?(solution1) :"
+ isRotation(string1, string2));
System.out.println();
System.out.println("Please enter another string1 (q to quit): ");
}
}
}
公共类IsRotation1\u 8{
/*
*想法:与CC150书中的代码相同
*首先,如果s2是s1的旋转,则其长度应相同
*然后,检查s2是否为s1+s1的子串
*
*时间复杂度:O(n)
*空间复杂度:O(n)
*/
私有静态布尔值isRotation(字符串s1、字符串s2){
如果(s1.length()!=s2.length()){
返回false;
}
字符串s3=s1+s1;
//返回问题字符串(s3、s2);
返回问题字符串2(s3、s2);
}
私有静态布尔isSubString(字符串s3、字符串s2){
返回s3.contains(s2);
//返回s3.indexOf(s2)!=-1;
}
/*
*在不使用Contains和indexOf方法的情况下实现isSubString方法
*/
私有静态布尔isSubString2(字符串s3、字符串s2){
int lengthS3=s3.length();
int lengthS2=s2.length();
对于(int i=0;i
它很好地实现了这些问题,但是我有一些关于它的问题
String.contains的实现以使用特定的算法。如果算法是,例如Knuth--Morris--Pratt,那么运行时间将是线性的。如果算法是朴素的,那么运行时间将是二次的
isSubString2
实现了最坏情况下的二次型朴素算法String
串联的成本等于组合字符串的长度,但每个StringBuilder
append的成本(摊销)等于附加字符串的长度。这里使用的单个String
串联始终是线性时间,但明智的做法是切换到StringBuilder
进行重复追加,以避免多次偿还第一个字符的成本典型的情况是O(n),但最坏的情况是O(n^2),无论您使用的是内置的方法还是StringBuffer/StringBuilder.1-2:如果要求是:不要使用String.contains(1.5)和String.indexOf(1.5之前),这意味着我应该使用KMP来实现这个Q?Boyer-Moore算法怎么样。有人告诉我BM算法比KMP快3-5倍。@jyuan你的目标是获得一个好的渐近最坏情况还是在非病理输入上快速运行?这是一个采访Q,因此,我认为我需要做的是使算法更高效、更简单。@jyuan你需要做的是向面试官提问,以引出额外的要求,从而让你做出适当的选择。