[英]find the occurrence of substrings in string given two list
Given two List sentence and List queries 给定两个列表语句和列表查询
I have two find the occurrence of queries in sentences. 我有两个发现句子中查询的发生。
Example, 例,
"Pulkit likes StackOverflow and coding" “ Pulkit喜欢StackOverflow和编码”
"Pulkit does not like Reddit" “ Pulkit不喜欢Reddit”
"Pulkit like ice cream" “ Pulkit像冰淇淋”
Queries 查询
Pulkit coding Pulkit编码
like 喜欢
does 不
The function should return for the queries 该函数应返回查询
sentence[0] 句子[0]
sentence[1], sentence[2] 句子[1],句子[2]
sentence[1] 句子[1]
I solved this question already using HashMap but it is quadratic and I am wondering how to do it in linear time. 我已经使用HashMap解决了这个问题,但是它是二次方的,我想知道如何在线性时间内做到这一点。
Solution 解
public static void findMatch(List<String> sentences, List<String> queries) {
// Write your code here
// Split the sentences into terms and map them by index
Map<Integer, Set<String>> sentencesSplit = new HashMap<>();
for (int j = 0; j < sentences.size(); j++) {
String[] splitSentence = sentences.get(j).split(" ");
Set<String> sentenceSet = new HashSet<>();
sentencesSplit.put(j, sentenceSet);
for (int i = 0; i < splitSentence.length; i++) {
sentenceSet.add(splitSentence[i]);
}
}
// Split the query into terms and map them by index
Map<Integer, String[]> queriesSplit = new HashMap<>();
for (int i = 0; i < queries.size(); i++) {
queriesSplit.put(i, queries.get(i).split(" "));
}
for (int i = 0; i < queries.size(); i++) {
String found = null;
for (int j = 0; j < sentences.size(); j++) {
String[] splitQuery = queriesSplit.get(i);
Set<String> sentenceStringList = sentencesSplit.get(j);
boolean notFound = false;
for (int k = 0; k < splitQuery.length; k++) {
if (!sentenceStringList.contains(splitQuery[k])) {
notFound = true;
break;
}
}
if (!notFound) {
if (found == null) {
found = "" + j;
} else {
found += " " + j;
}
}
}
if (found == null) {
found = "-1";
}
System.out.println(found);
}
}
My code is similar with human's thinking. 我的代码与人类的思想相似。
\\b allows you to perform a "whole words only" search using a regular expression in the form of \\bword\\b. \\ b允许您使用\\ bword \\ b形式的正则表达式执行“仅整个单词”搜索。
Hope my code will help you. 希望我的代码能对您有所帮助。
public class MainClass {
public static void main(String [] args)
{
List<String> sentences = new ArrayList<String>();
sentences.add("Pulkit likes StackOverflow and coding");
sentences.add("Pulkit does not like Reddit");
sentences.add("Pulkit like ice cream");
List<String> queries = new ArrayList<String>();
queries.add("Pulkit coding");
queries.add("like");
queries.add("does");
findMatch(sentences, queries);
}
public static void findMatch(List<String> sentences, List<String> queries) {
for(String query : queries) {
System.out.print("]");
String match = ".*\\b" + query.replace(" ", "\\b.*") + "\\b.*";
for (int iSentence = 0; iSentence < sentences.size(); iSentence++) {
if(sentences.get(iSentence).matches(match)) {
System.out.print(" " + iSentence);
}
}
System.out.println("");
}
}
}
Console output: 控制台输出:
] 0
] 1 2
] 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.