-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhichAreIn.java
36 lines (34 loc) · 1.05 KB
/
WhichAreIn.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package me.m_zebrak.kyu6;
import java.util.stream.Stream;
/**
* Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which
* are substrings of strings of a2.
* <p>
* Example 1:
* a1 = ["arp", "live", "strong"]
* <p>
* a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
* <p>
* returns ["arp", "live", "strong"]
* <p>
* Example 2:
* a1 = ["tarp", "mice", "bull"]
* <p>
* a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
* <p>
* returns []
* <p>
* Notes:
* Arrays are written in "general" notation. See "Your Test Cases" for examples in your language.
* In Shell bash a1 and a2 are strings. The return is a string where words are separated by commas.
* Beware: r must be without duplicates.
*/
public class WhichAreIn {
public static String[] solution(String[] a, String[] b) {
return Stream.of(a)
.filter(x -> Stream.of(b).anyMatch(y -> y.contains(x)))
.distinct()
.sorted()
.toArray(String[]::new);
}
}