Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int longestStrChain(String[] words) {
- Arrays.sort(words, (s1, s2) -> s1.length() - s2.length());
- int ans = 0;
- Map<String, Integer> wordsMap = new HashMap();
- for (String word : words) {
- int maxLen = 0;
- for (int j = 0; j < word.length(); j++) {
- String s = word.substring(0, j) + word.substring(j+1);
- maxLen = Math.max(maxLen, wordsMap.getOrDefault(s, 0) + 1);
- }
- wordsMap.put(word, maxLen);
- ans = ans < maxLen ? maxLen : ans;
- }
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement