/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
import java.util.stream.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String testString = "a b c d ab ac ad da ca ba d e b z s aa";
		long startTime;
		
		
		String[] keywords = {"a", "ab", "ad", "c", "d", "j", "k", "sz", "hk", "ok"};
		List<Pattern> patterns = Arrays.asList(keywords).stream().map(keyword -> Pattern.compile("\\b"+keyword+"\\b")).collect(Collectors.toList());
		startTime = System.nanoTime();
		for (Pattern p : patterns) {
			Matcher m = p.matcher(testString);
			while (m.find()) { System.out.println(m.group()); }
		}
		System.out.printf("Took %s ms%n", (System.nanoTime() - startTime) / 1000);
		
		Pattern multiPattern = Pattern.compile("\\b(a[bd]?|c|d|j|[oh]?k|sz)\\b");
		startTime = System.nanoTime();
		Matcher m = multiPattern.matcher(testString);
		while (m.find()) { System.out.println(m.group()); }
		System.out.printf("Took %s ms%n", (System.nanoTime() - startTime) / 1000);
	}
}