给出两个列表<;list<;字符串>;>;(称为a和b).
它返回:map<;list<;字符串>;,list<;list<;字符串>;>;
- 让a,b的项分别是a1,a2,a3,……B1,b2,b3...
- 仅 Select b1和a1元素中具有重叠字符串的项目(列表<;字符串>;)
- 在结果中输入Key=b1,Value=a1.
(前)
Define a and b as follows:
a = [a, b, c], [d, e, f], [a, d, f]
b = [a, d], [a], [c], [x]
it returns :
key value
[a,d] | [a,b,c],[d,e,f],[a,d,f]
[a] | [a,b,c],[a,d,f]
[c] | [a,b,c]
[x] | empty list
In reality, the length of the list in a and b will exceed at least 100,000.
I tried this method using the List.contains, but time complexity was O(n^3) at the worst case.
Here is my code, I want to reduce the time complexity of that algorithm below O(n^2).
public Map<List<String>, List<List<String>>> compute(List<List<String>> a, List<List<String>> b) {
Map<List<String>, List<List<String>>> result = new HashMap<>();
for (List<String> elem : b) {
result.put(elem, a.stream().filter(e -> e.stream().anyMatch(elem::contains)).toList());
}
return result;
}