简体   繁体   English

代码验证和优化-两个String公用子字符串

[英]Code verifcation and optimization - Two String common substring

I was solving Two String problem. 我正在解决“ 两个字符串”问题。 I have written below code. 我写了下面的代码。 It passed 4 test cases but for two test cases it showed timeout. 它通过了4个测试用例,但有两个测试用例显示超时。 Kindly let me know how can I optimize it to avoid timeouts? 请让我知道如何优化它以避免超时? Also any links which explains and shows examples of such optimization is welcome. 也欢迎任何解释和显示此类优化示例的链接。

  public class TwoStrings
{
     private static final String YES = "YES";
private static final String NO  = "NO";

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
String input1[] = new String[testCases];
String input2[] = new String[testCases];

for (int i = 0; i < testCases; i++)
{
    input1[i] = in.nextLine();
    input2[i] = in.nextLine();
}
in.close();

for (int i = 0; i < testCases; i++)
{
    displayResult(input1[i], input2[i]);
}
}

private static void displayResult(String string1, String string2)
{
// choosing smaller String for iterating through it.
String smallerString = string1.length() <= string2.length() ? string1
    : string2;
String biggerString = string1 == smallerString ? string2 : string1;

boolean constains = false;

// Concept - Even if single letter is common, substring exists.
// So checking just one string.
for (int i = 0; i < smallerString.length(); i++)
{
    if (biggerString.contains(String.valueOf(smallerString.charAt(i))))
    {
    constains = true;
    break;
    }
}

if (constains)
    System.out.println(YES);
else
    System.out.println(NO);
}
}

What you are currently doing is O(n^2) because you loop through the small string and the search for that character in the longer string is a linear search because it is not sorted (all letters in alphabetical order). 您当前正在执行的操作是O(n ^ 2),因为您在小字符串中循环,而在较长字符串中对该字符的搜索是线性搜索,因为它没有排序(所有字母均按字母顺序排列)。

Below is a O(n) solution. 以下是O(n)解决方案。 The concept is to have a size 26 boolean array (one for each letter), and make an index true if a letter is in the small (could actually be small or long string, doesn't matter) string. 这个概念是要有一个大小为26的布尔数组(每个字母一个),并且如果一个字母在小(实际上可以是小或长字符串,无所谓)字符串中,则使索引为true。 Creating the array from the small string is O(n), and checking the letters in the long string is O(n), yielding a grand total of O(n + n), which reduces to O(n). 从小字符串创建数组是O(n),检查长字符串中的字母是O(n),得出的总数为O(n + n),总和减少为O(n)。

private static void displayResult(String string1, String string2)
{
    boolean[] contains = new boolean[26];
    boolean noSubstring = true;

    // populate the contains array
    for (char c : string1.toCharArray())
    {
        int value = (int)c - (int)'a';    // make the char 0-25
        contains[value] = true;
    }

    for (char c : string2.toCharArray())
    {
        int value = (int)c - (int)'a';    // make the char 0-25

        if (contains[value])
        {
            noSubstring = false;
            break;
        }
    }

    if (noSubstring)   System.out.println("NO");
    else               System.out.println("YES");
}

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

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