Arrays.stream(elements).reduce(str, (r, w) -> r.replace(w, ""))
with the expected output.
If you want to reduce the input string until it is no longer possible, it is best to iterate until there are no changes
String n = str, o = null;
do {
n = stream(elements).reduce(o = n, (r, w) -> r.replace(w, ""));
} while(!n.equals(o));
System.out.println(n);
then, with input string
This is a caterpillar and that is a docatg.
you'll get
This is a erpillar and that is a .
If really want a fast algorithm use Aho-Corasick with cost O(n)
StringBuilder sb = new StringBuilder();
int begining = -1;
for (Emit e : Trie.builder().addKeywords(elements).build().parseText(str)) {
sb.append(str, begining + 1, e.getStart());
begining = e.getEnd();
}
sb.append(str, begining + 1, str.length());
System.out.println(sb.toString());
Aside solution performance comparison (with Oussama ZAGHDOUD's solution):
Equals = true // check all output are equals
Time1 = 18,548822 // Oussama ZAGHDOUD's solution O(n^2)
Time2 = 0,134459 // Aho-Corasick O(n) without precompute Trie
Time3 = 0,065056 // Aho-Corasick O(n) precomputed Trie
full bench code
static String alg1(String[] elements, String str) {
StringBuilder bf = new StringBuilder(str);
str =null;
Stream.of(elements).forEach(e -> {
int index = bf.indexOf(e);
while (index != -1) {
index = bf.indexOf(e);
if (index != -1) {
bf.delete(index, index + e.length());
}
}
});
return bf.toString();
}
static String alg2(String[] elements, String str) {
StringBuilder sb = new StringBuilder();
int begining = -1;
for (Emit e : Trie.builder().addKeywords(elements).build().parseText(str)) {
sb.append(str, begining + 1, e.getStart());
begining = e.getEnd();
}
sb.append(str, begining + 1, str.length());
return sb.toString();
}
static String alg3(Trie trie, String str) {
StringBuilder sb = new StringBuilder();
int begining = -1;
for (Emit e : trie.parseText(str)) {
sb.append(str, begining + 1, e.getStart());
begining = e.getEnd();
}
sb.append(str, begining + 1, str.length());
return sb.toString();
}
public static void main(String... args) throws JsonProcessingException {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
// test, use random numbers as words
String[] elements = range(0, 1_000).mapToObj(i -> "w" + rnd.nextInt()).toArray(String[]::new);
// intercalate random elements word with other random word
String str = range(0, 100_000)
.mapToObj(i -> "z" + rnd.nextInt() + " " + elements[rnd.nextInt(elements.length)])
.collect(joining(", "));
Trie trie = Trie.builder().addKeywords(elements).build();
long t0 = System.nanoTime();
String s1 = alg1(elements, str);
long t1 = System.nanoTime();
String s2 = alg2(elements, str);
long t2 = System.nanoTime();
String s3 = alg3(trie, str);
long t3 = System.nanoTime();
System.out.printf("Equals = %s%nTime1 = %f%nTime2 = %f%nTime3 = %f%n",
s1.equals(s2) && s2.equals(s3), (t1 - t0) * 1e-9, (t2 - t1) * 1e-9, (t3 - t2) * 1e-9);
}
"docatg"(dog with cat in the middle of it)? Do you want to first remove the cat, then remove the dog? Or do you want to just remove the cat?elements = {"cat", "catcher",...}With a phrase "pass it to catcher", if "cat is removed first, then the phrase will be "pass it to her"["app","ply"]With the phrase "apply", we could remove "app" and leave "ly", or remove "ply" and leave "ap".