If you simply want to connect the tokens into a list of correctly spelled words then you proceed as follows (Top down style dynamic programming):
public class Connector {
public static void main(String[] args) {
List<String> resultstokens = new ArrayList<>ArrayList<String>(); // resultsinput
are stored here Connector connector = new Connector();
@SuppressWarnings("unused")
List<String> ret = connector.connect(tokens,);
results }
Map<Integer, "");List<String>> //cache;
the call List<String> tokens;
void public List<String> connect(List<String> tokens) {
this.cache = new HashMap<Integer, List<String>>();
this.tokens = tokens;
List<String> resultsret = connect(tokens, String0);
prefix return ret;
}
List<String> connect(List<String> tokens, int start) {
if (tokenscache.sizecontainsKey(start)) =={
0 return cache.get(start);
} else {
List<String> results = new ArrayList<String>();
connectHelper(tokens, start, results);
cache.addput(prefixstart, results);
return; return results;
}
}
void connectHelper(List<String> tokens, int start, List<String> results) {
int n = tokens.size();
for(int i = start + 1; i < n; i++) {
String part = concat(tokens, start, i);
if (dictContains(part)) {
List<String> restParts = connect(tokens.subList(, i);
+ 1, n for(String oneRestPart : restParts), results,{
prefix results.add(part + " " + partoneRestPart);
}
}
}
}
static private String concat(List<String> tokens, int nlow, int hi) {
StringBuilder sb = new StringBuilder();
for(int i = 0;low; i < n;hi; i++) {
sb.append(tokens.get(i));
}
return sb.toString();
}
}