Counting the max string length in the dictionary and terminating the inner loop early speeds it up from 9ms to 2ms.

```
public boolean wordBreak(String s, Set<String> wordDict) {
int[] mem = new int[s.length()+1];
mem[0] = 1;
int max = 0;
for (String word: wordDict)
max = Math.max(max,word.length());
for (int i = 0; i < s.length(); i++)
if (mem[i] == 1)
for (int j = i; j < mem.length && j - i <= max; j++)
if (wordDict.contains(s.substring(i,j)))
mem[j] = 1;
return mem[s.length()] == 1;
}
```

Ugly bonus:

```
public boolean wordBreak(String s, Set<String> wordDict) {
if (wordDict.size() == 0) return false;
short[] mem = new short[s.length()+1];
mem[0] = 1;
IntStream.range(0, s.length())
.forEach(i -> {
if (mem[i] == 1)
IntStream.range(i, s.length() + 1)
.filter(j -> wordDict.contains(s.substring(i,j)))
.forEach(j -> mem[j] = 1);
});
return mem[s.length()] == 1;
}
```