DP + HashMap
class Solution {
public int longestStrChain(String[] words) {
Map<String, Integer> map = new HashMap<>();
Arrays.sort(words, (w1, w2) -> w1.length() - w2.length());
int longest = 1;
for (String word : words) {
int presentLen = 1;
for (int i = 0; i < word.length(); i++) {
StringBuilder sb = new StringBuilder(word);
sb.deleteCharAt(i);
String str = sb.toString();
presentLen = Math.max(presentLen, map.getOrDefault(str, 0) + 1);
}
map.put(word, presentLen);
longest = Math.max(longest, presentLen);
}
return longest;
}
}