-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path1048. Longest String Chain
33 lines (29 loc) · 1.13 KB
/
1048. Longest String Chain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Time : NlogN + N * Length * Length
//Space : O(N*Length)
class Solution {
public int longestStrChain(String[] words) {
//Sort on length
//Time : NlogN
Arrays.sort(words, (a,b) -> a.length()-b.length());
int res = 0;
Map<String, Integer> memo = new HashMap<>();
//Iterate on the words
//TIme : N * Length * Length
for(String word : words) {
//Put current word in map with default value.
memo.put(word, 1);
//Time : Length * Length
for(int i = 0; i < word.length(); i++) {
StringBuilder current = new StringBuilder(word);
String next = current.deleteCharAt(i).toString(); //Time : Length
//Check if the value for next is already calculated
if(memo.containsKey(next)) {
//Update the value in map with the maximum possible value
memo.put(word, Math.max(memo.get(word), memo.get(next)+1));
}
}
res = Math.max(res, memo.get(word));
}
return res;
}
}