Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Create all possible substrings of a 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;
}
Source by www.byte-by-byte.com #
 
PREVIOUS NEXT
Tagged: #Create #substrings #string #java
ADD COMMENT
Topic
Name
2+2 =