public static boolean f(List l, String s) { if (l.contains(s) || s.isEmpty()) { return true; } else { boolean result = false; for (String elem : l) { if (s.startsWith(elem)) { result = result || f(l, s.substring(elem.length())); if (result) { return true; } } } return false; } }