简体   繁体   中英

How do I reduce the time complexity for the following code?

How do I reduce the Time Complexity for the following code?

I have to check if the words in the queries are present in the sentences and print the sentence number else print "-1".

The order of the words does not matter in the queries. They should be present in the sentence, that's it.

class Result {

static boolean o=false;    

public static void textQueries(List<String> sentences, List<String> queries) {
// Write your code here
    String[] s, se;
    ArrayList<HashSet<Integer>> arr=new ArrayList<>();

    for(int i=0; i<queries.size(); i++) {
        s=queries.get(i).split(" ");
        HashSet<String> set=new HashSet<>(Arrays.asList(s));
        for(String str : set) {
            HashSet<Integer> q=new HashSet<>();
            for(int j=0; j<sentences.size(); j++) {
                se=sentences.get(j).split(" ");
                HashSet<String> sen=new HashSet<>(Arrays.asList(se));
                if(sen.contains(str)) {
                    q.add(j);
                }
            }
            arr.add(q);
        }
        for(int d=0; d<sentences.size(); d++) {
            print(d, arr);
        }
        if(o==false)
            System.out.print("-1");
        System.out.println();
        arr.clear();
        o=false;
    }

}

public static void print(int d, ArrayList<HashSet<Integer>> arr) {
    for(int a=0; a<arr.size(); a++) {
        HashSet<Integer> st=arr.get(a);
        if(!st.contains(d))
            return;
    }
    o=true;
    System.out.print(d+" ");
}

}

Here is what I think is an improved version:

    import java.util.Arrays;
    import java.util.List;

    public class Main {
        public static void main(String[] args) {
            List<String> sentences = Arrays.asList("a bc def", "bc, ajj");
            List<String> queries = Arrays.asList("a bc def");
            textQueries(sentences, queries);
        }

        public static void textQueries(List<String> sentences, List<String> queries) {
            int counter = 0;
            outer:
            for (String sentence : sentences) {
                System.out.println("Process sentence: " + sentence + " nr. " + (++counter));
                List<String> sentenceWords = Arrays.asList((sentence.split(" ")));
                for (String query : queries) {

                    if (sentenceWords.containsAll(Arrays.asList((query.split(" "))))) {
                        System.out.println(counter);
                        continue outer;
                    }
                }
                System.out.println(-1);
            }
        }
    }
public static void textQueries(List<String> sentences, List<String> queries) {
    final List<Set<String>> sentencesWords = getSentenceUniqueWords(sentences);
    final Function<Set<String>, Set<Integer>> findSentencesWithAllWords = words -> {
        int pos = 0;
        Set<Integer> positions= new TreeSet<>();

        for (Set<String> sentenceWords : sentencesWords) {
            if (sentenceWords.containsAll(words))
                positions.add(pos);

            pos++;
        }

        return positions;
    };

    for (String query : queries) {
        Set<Integer> positions= findSentencesWithAllWords.apply(getUniqueWords(query));

        if (positions.isEmpty())
            System.out.println(-1);
        else
            System.out.println(positions.stream()
                                  .map(String::valueOf)
                                  .collect(Collectors.joining(" ")));
    }
}

private static List<Set<String>> getSentenceUniqueWords(List<String> sentences) {
    return sentences.stream()
                    .map(sentence -> getUniqueWords(sentence))
                    .collect(Collectors.toList());
}

private static final Pattern SPACES = Pattern.compile("\\s+");

private static Set<String> getUniqueWords(String str) {
    return Arrays.stream(SPACES.split(str)).collect(Collectors.toSet());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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