简体   繁体   English

无法在自动完成中通过测试用例

[英]unable to pass test case in autocomplete

Question Autocomptete问题自动完成

Doug was using Google and was amazed to see the autocomptete feature How autocomptete works it search the database for all the possible words that can be formed using the characters that are provided by user (as input) Doug 正在使用 Google 并惊讶地看到自动比较功能自动比较的工作原理它在数据库中搜索所有可能的单词,这些单词可以使用用户提供的字符(作为输入)形成

For ex If a user type 'cis' in the search bar then suggestions would be例如,如果用户在搜索栏中输入“cis”,那么建议将是

• cisco • 思科

• cist • cist

• cissp • 顺式

• cism • 顺化

• cisa He thought about applying the same feature in his search engine. • cisa 他考虑在他的搜索引擎中应用相同的功能。 In his prototype he took a string as domain which contained all the words he could search.在他的原型中,他将一个字符串作为域,其中包含他可以搜索的所有单词。 As his designer you have to tell him how many autocomptete options will be provided to him if something is entered in the input field.作为他的设计师,您必须告诉他,如果在输入字段中输入内容,将向他提供多少自动完成选项。

This is my code for the following problem.这是我针对以下问题的代码。

import java.util.ArrayList;
import java.util.List;


public class Test {

public static void main(String[] args) {


    String input1 = "Hello world with warm welcome from Mr.kajezevu";
    String input2 = "w";
    //output should be any word starting with w i.e {world,warm,welcome}




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



    String[] str = input1.split("\\s+");//splits a given string at spaces
    for (int i = 0; i < str.length; i++) {
        if (str[i].length() >= input2.length()) { // checks if the length of input2 is not greater than the str[i] selected
            if (input2.equals(str[i].substring(0, input2.length()))) { //comparing the two string if they are equal
                l.add(str[i]);
            }

        }
    }

    String[] result = l.toArray(new String[l.size()]);

    for (int i = 0; i < result.length; i++) {
        System.out.println(result[i]);
    }
}


}

But my solution is passing only one test case and also its failing complexity case.但是我的解决方案只通过了一个测试用例以及它失败的复杂性用例。 i can't figure out whats wrong with it.我不知道有什么问题。

It seems you missed boundary conditions.看来您错过了边界条件。 Below is code.下面是代码。

public static String[] autoComplete(String input1, String input2){
    List<String> listOfPredictions = new ArrayList<String>();
    String[] emptyArr = new String[0];
    if(isEmpty(input1) || isEmpty(input2)){
        return emptyArr;
    } 
    input1 = input1.trim();
    input2 = input2.trim();
    String tokenizer = " " + input2;
    int fromIdx = 1;
    if(input1.startsWith(input2)){
        fromIdx = input1.indexOf(" ");
        listOfPredictions.add(input1.substring(0, fromIdx));
    }

    while(fromIdx > 0){
        fromIdx = input1.indexOf(tokenizer, fromIdx) + 1;
        if(fromIdx > 0){
            listOfPredictions.add(input1.substring(fromIdx, input1.indexOf(" ", fromIdx)));
        }
    }

    return listOfPredictions.toArray(emptyArr);
}

private static boolean isEmpty(String str){
    return str == null || str.trim().length() == 0;
}

We also need to remove all duplicate words from the resulting array.我们还需要从结果数组中删除所有重复的单词。 So first we break the string into words using the string.split() function.所以首先我们使用string.split()函数将字符串分解成单词。 Then push all those words that start with input2 string.然后推送所有以 input2 字符串开头的单词。 Then from the resulting array, we remove all duplicates by creating a Set and then converting it back into an Array .然后从结果数组中,我们通过创建一个Set并将其转换回一个Array删除所有重复项。

function autoComplete(input1, input2) {
   let results = [];
   if(!input1 || !input1.length || !input2 || !input2.length) return results;
   input1 = input1.trim();
   input2 = input2.trim();
   let allWords = input1.split(/\s+/);
   allWords.forEach(word => {
      if(word.startsWith(input2)) {
          results.push(word);
      }
   })
   results = [...[...new Set(results)]];
   return results;
}

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

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