Given a string find the number of meaningful words which could be formed from the string for eg. programmerit forms:
pro+gram+merit program+merit programmer+it pro+grammer+it
Looking for code review optimizations and best practices. Also hoping for suggestions for complexity better than \$O(n^2)\$, where n is the number of characters.
public final class DictionaryValidWords {
private static final Set<String> dictionary = new TreeSet<String>();
static {
dictionary.add("this");
dictionary.add("his");
dictionary.add("is");
dictionary.add("awe");
dictionary.add("we");
dictionary.add("some");
dictionary.add("awesome");
dictionary.add("foo");
dictionary.add("bar");
}
private DictionaryValidWords() {}
/**
* Returns set of valid words given an input string.
* It eliminates duplicates.
*
* @param str The input string whose valid words need to be found out.
* @return List of valid words nested in the string.
*/
public static Set<String> findValidStrings(String str) {
if (str.length() == 0) {
throw new IllegalArgumentException("Strings of length 0 are illegal");
}
final Set<String> validWords = new HashSet<String>();
for (int i = 0; i < str.length(); i++) {
StringBuilder sb = new StringBuilder();
for (int j = i; j < str.length(); j++) {
sb.append(str.charAt(j)); // O(1) complexity.
if (dictionary.contains(sb.toString())) {
validWords.add(sb.toString());
}
}
}
return validWords;
}
}
public class DictionartValidWordTest {
@Test
public void testValidWords() {
Set<String> expectedSet = new HashSet<String>(Arrays.asList("awe", "is", "his", "awesome", "some", "this", "we"));
assertEquals(expectedSet, DictionaryValidWords.findValidStrings("thisisawesome"));
}
}