[英]Trying to design a function : hasCheated(String s1,String s2, int N) that evaluates
I got this question: A professor wants to see if two students have cheated when writing a paper. 我有一个问题:一位教授想看看两个学生在写论文时是否被骗了。 Design a function : hasCheated(String s1,String s2, int N) that evaluates to true if two strings have a common substring of length N. Additional question after implementation.
设计一个函数:hasCheated(String s1,String s2,int N),如果两个字符串的公共子字符串长度为N,则结果为true。实现后的附加问题。 Assume you don't have the possibility of using String.contains() and String.substring().
假设您无法使用String.contains()和String.substring()。 How would you implement this?
您将如何实施?
this is how I am trying to solve it: 这就是我试图解决的方法:
public class exercise {
public static void main(String[] args){
String s1 ="";
String s2 = "";
int n=0;
boolean s3;
for(int i=0; i<=s1.length();i++){
if(i+n <=s1.length()){
if(s3=s1.contains(s2.substring(i, i+n)));
System.out.printf("HasCheated\n ",s3);
//return true;
}
}
//return false;
}
}
My Quiz is, I am doing the correct thing? 我的测验是,我在做正确的事情? Thanks in advance.
提前致谢。
First change this: <=s1.length()
to <s1.length()
, otherwise you will get an ArrayOutOfIndex
Exception. 首先将此更改:
<=s1.length()
更改为<s1.length()
,否则您将获得ArrayOutOfIndex
异常。
You can use indexOf()
. 您可以使用
indexOf()
。
So Basically this would be my approach: 所以基本上这是我的方法:
@Pritam is right if you want to do it that way, but, assuming you don't have the possibility of using String.contains() and String.substring(). 如果要这样做,@ Pritam是正确的,但是,假设您无法使用String.contains()和String.substring()。 This is how i would do it.
这就是我会做的。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class exercise{
public static void main(String[] args) {
String s1="home";
String s2="homework";
int n=4;
exercise p=new exercise();
if(p.hasCheated(s1, s2, n))
{
System.out.println("Student Cheated ");
return;
}
System.out.println("Not Cheated");
}
public boolean hasCheated(String s1,String s2, int N)
{
boolean s3=true;
ArrayList<String> al=new ArrayList<String>();
ArrayList<String> bl=new ArrayList<String>();
al.addAll(getInfo(s1,N));
bl.addAll(getInfo(s2,N));
al.retainAll(bl);
if(al.size()==0)
{
s3=false;
}
return s3;
}
public List<String> getInfo(String s,int n)
{
ArrayList<String> inf=new ArrayList<String>();
inf.clear();
String myStr=Arrays.toString(s.split("(?<=\\G.{4})"));
Scanner sc=new Scanner(myStr).useDelimiter(",");
while(sc.hasNext())
{
String myString=sc.next().replaceAll("\\W", "");
inf.add(myString);
}
return inf;
}
}
by your logic, it would make more sens to write 按照您的逻辑,写起来会更有意义
if(s3=s2.contains(s1.substring(i, i+n)));
since you are iterating through your first sprint, you are trying to figure out if any of the substrings of length N in s1 is contained in s2. 由于您正在遍历第一个Sprint,因此您尝试确定s2中是否包含s1中长度为N的任何子字符串。
You boolean is not necessary, it does not add any value to the code. 布尔值不是必需的,它不会在代码中添加任何值。
public hasCheated(String s1, String s2, int n){ for(int i=0; i < s1.length; i++){ if(i + n < s1.length){ if(s2.contains(si.substring(i,i+n))){ return true; } } } return false }
Without contains or substring, you could use indexOf() as Pritam mentioned, but you would still need to replace the substring. 如果没有contains或substring,则可以使用Pritam所述的indexOf(),但仍需要替换substring。 To do so, you couls convert your string to charArray and work with indexes.
为此,您可以将字符串转换为charArray并使用索引。 It does increase the complexity.
确实增加了复杂性。
public hasCheated(String s1, String s2, int n){
char[] tempCharArray;
char[] charString = s1.toCharArray();
String comparedString = "";
for(int i=0; i < s1.length; i++){
if(i + n < s1.length){
for(int j = 0; j<n ; j++){
tempCharArray[j] = charString[i+j];
}
comparedString = String(tempCharArray);
if(s2.indexOf(comparedString) > 0){
return true;
}
}
}
return false
}
I havn't looked closely at my code, or if any better methods existed, but this would work 我没有仔细查看我的代码,或者是否存在更好的方法,但这是可行的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.