Erstellen Sie alle möglichen Substrings einer String -Java
public int delete(String query, HashSet dictionary) {
Queue queue = new LinkedList();
Set queueElements = new HashSet();
queue.add(query);
queueElements.add(query);
while (!queue.isEmpty()) {
String s = queue.remove();
queueElements.remove(s);
if (dictionary.contains(s)) return query.length() - s.length();
for (int i = 0; i < s.length(); i++) { String sub = s.substring(0, i) + s.substring(i+1, s.length()); if (sub.length() > 0 && !queueElements.contains(sub)) {
// this for loop creates all possible substrings
queue.add(sub);
queueElements.add(sub);
}
}
}
return -1;
}
Clever Crossbill