简体   繁体   English

给定两个字符串,确定它们是否共享一个共同的 substring

[英]Given two strings, determine if they share a common substring

This is my first Question here, I need to know why the following code does not pass Sample test case 2 in hacker rank-> Algorithms-> Strings-> Two Strings: The question is here: https://www.hackerrank.com/challenges/two-strings/problem这是我在这里的第一个问题,我需要知道为什么以下代码没有通过 Hacker rank-> Algorithms-> Strings-> Two Strings 中的示例测试用例 2:问题在这里: https://www.hackerrank.com /挑战/两个字符串/问题

public static String twoStrings(String s1, String s2) {
        String answer = ""; 
        String StringToIterate = "";
        String theOtherString = "";
    
        if (s1.length() > s2.length()) {
            StringToIterate = s2;
            theOtherString  = s1;
        } else if (s1.length() < s2.length()){
            StringToIterate = s1;
            theOtherString = s2;
        } else {
            StringToIterate = s1;
            theOtherString = s2;
        }
    
    for (int i= 0; i < StringToIterate.length();i++) {
         String subS = StringToIterate.substring(i);
         if (theOtherString.contains(subS)) {
            answer = "YES";
        } else {
            answer = "NO";
       }
   }
   return answer;
   }
}

Sample test case 2:示例测试用例 2:
2 2

aardvark

apple

beetroot

sandals

my code gives: No No but the expected output is: Yes No我的代码给出:No No 但预期的 output 是:Yes No

To get this sample case working I would suggest replacing answer = "YES";为了让这个示例案例正常工作,我建议替换answer = "YES"; with return "YES"; return "YES"; . .

There only has to be one substring that matches.只需要一个匹配的 substring。 Therefore, you should stop searching when you have found a match.因此,您应该在找到匹配项后停止搜索。 Your code will happily continue searching through other substrings, even if you already found a match, and thereby setting the answer back to NO .即使您已经找到匹配项,您的代码也会愉快地继续搜索其他子字符串,从而将answer设置回NO

I don't know what the exact assignment was, but it seems that you also have to look at internal substrings.我不知道确切的分配是什么,但似乎您还必须查看内部子字符串。 Your code only looks at the first i letters of StringToIterate .您的代码仅查看StringToIterate的前 i 个字母。

I try to explain it using your sample case: Your code will substring check these strings我尝试使用您的示例案例来解释它:您的代码将 substring 检查这些字符串

a
ap
app
appl
apple

However, p or le could've also been substrings of aardvark.然而, ple也可能是土豚的子串。 Try to change your code to also check these substrings.尝试更改您的代码以检查这些子字符串。

I hope this is enough information to keep on coding and good luck with your assignment!我希望这是足够的信息来继续编码并祝你的作业好运!

The initial Test cases passed by doing the suggestions given (adding break; after "YES" and use substring(i,i+1)), however, when I submitted the following code, I got "Time limit exceeded" problem.通过执行给出的建议(添加中断;在“YES”之后并使用 substring(i,i+1))通过了初始测试用例,但是,当我提交以下代码时,出现“超出时间限制”问题。

public static String twoStrings(String s1, String s2) {
        String answer = ""; 
        String StringToIterate = "";
        String theOtherString = "";

        if (s1.length() > s2.length()) {
            StringToIterate = s2;
            theOtherString  = s1;
        } else if (s1.length() < s2.length()){
            StringToIterate = s1;
            theOtherString = s2;
        } else {
            StringToIterate = s1;
            theOtherString = s2;
        }

    for (int i= 0; i < StringToIterate.length();i++) {
         String subS = StringToIterate.substring(i,i+1);
         if (theOtherString.contains(subS)) {
            answer = "YES";
            break;
        } else {
            answer = "NO";
       }
   }
   return answer;
   }
}

I cannot add return inside the for loop/if statement,because the compiler of course wants a return in the method body but outside the for loop.我不能在 for 循环/if 语句中添加 return,因为编译器当然希望在方法体中但在 for 循环之外返回。 So, any suggestion?那么,有什么建议吗?

I'm assuming one of the test cases uses a fairly large string with a lot of duplicate letters.我假设其中一个测试用例使用了一个相当大的字符串,其中包含很多重复的字母。 You can try editing your solution to keep track of substrings you've already checked.您可以尝试编辑您的解决方案以跟踪您已经检查过的子字符串。 For example:例如:

public static String twoStrings(String s1, String s2) {
    String answer = ""; 
    String StringToIterate = "";
    String theOtherString = "";

    List<String> checked = new ArrayList<>();

    if (s1.length() > s2.length()) {
        StringToIterate = s2;
        theOtherString  = s1;
    } else if (s1.length() < s2.length()){
        StringToIterate = s1;
        theOtherString = s2;
    } else {
        StringToIterate = s1;
        theOtherString = s2;
    }

    for (int i= 0; i < StringToIterate.length();i++) {
        String subS = StringToIterate.substring(i,i+1);
        if (checked.contains(subS)) {
            continue;
        }

        if (theOtherString.contains(subS)) {
            answer = "YES";
            break;
        } else {
            checked.add(subS);
            answer = "NO";
        }
    }

    return answer;
}

Running your function with the checked List does not run into the time limit.使用checked的列表运行 function 不会超出时间限制。

But this got me thinking "a Stream can do all of this " and that had me solving this problem in a completely different manner:但这让我想到“Stream 可以完成所有这些”,这让我以完全不同的方式解决了这个问题:

public static String twoStrings(String s1, String s2) {
    return s1.chars()
             .distinct()
             .mapToObj(c -> String.valueOf((char)c))
             .anyMatch(s2::contains) ? "YES" : "NO";
}

Without the .distinct() step I also get a timeout on tests 4 and 5, but with it all tests pass reasonably quickly.如果没有.distinct()步骤,我也会在测试 4 和 5 上超时,但是所有测试都很快通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM