Hello,
It is mentioned at the bottom of the word brea problem that use a set to avoid duplicate calls. I implemented the solution from this feedback, but no improvement in time complexity.
Could you check if I implemented it right?
class Solution {
public boolean wordBreak(String s, List wordDict) {
HashSet deadEnd = new HashSet<>();
for (int i = 1; i <= s.length(); ++i) {
String first = s.substring(0, i);
if (wordDict.contains(first)) {
String second = s.substring(i);
if(!deadEnd.contains(second)){
if (second == null || second.length() == 0 || wordDict.contains(second) || wordBreak(second, wordDict)) {
return true;
}else{
deadEnd.add(second);
}
}
}
}
return false;
}
}