简体   繁体   English

尝试设计一个函数:hasCheated(String s1,String s2,int N)求值

[英]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;
}


}
  1. 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的任何子字符串。

  2. 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.

相关问题 String s1 == String s2(true)但FieldOffset不同 - String s1 == String s2 (true) but FieldOffset is different 为什么哈希码对于String s1 =“cat”和String s2 = new String(“cat”)是相同的? - Why hash code is same for String s1= “cat” and String s2= new String(“cat”)? 使用可比较或比较器接口使用String s1的顺序对String s2进行排序 - Sort String s2 using the order of String s1 using either of comparable or comparator interface String s = s1 + s2;(s1和s2是字符串文字)从何处返回? 堆或池 - Where does String s= s1+s2;(s1 & s2 are String literals) returns from? heap or pool 在连接两个String s1和s2时,生成的输出String s3将在java中创建堆或常量池吗? - on concatenating two String s1 and s2, produced output String s3 will create in heap or constant pool in java? S1包含带有正则表达式的s2 - S1 contains s2 with regex 改进我的Java方法containsSubstring(s1,s2),它查找s2是否为s1的子字符串 - Improving my Java method containsSubstring(s1, s2) which finds if s2 is a substring of s1 为什么选择System.out.println(“嘿s1 == s2:”+ s1 == s2); 打印“false”作为输出而不是打印“hey s1 == s2:false” - Why System.out.println(“hey s1==s2:”+s1==s2); prints “false” as the output instead of printing “hey s1==s2:false” 返回List的方法 <T> 带参数列表 <S1> ,列表 <S2> ,其中S1和S2延伸T. - Method returning a List<T> with parameters List<S1>, List<S2>, where S1 and S2 extends T 如何更改Student类,以便当s1 = new Student()和s2 = new Student()时,s1 == s2返回true? - How to change Student class so that s1 == s2 return true when s1 = new Student() and s2 = new Student()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM