-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1048. Longest String Chain.java
81 lines (66 loc) · 2.23 KB
/
1048. Longest String Chain.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
https://leetcode.com/problems/longest-string-chain/
解法一:DP
时间复杂度:O(n log n + n^2 * l), n=words.length, l=words[i].length()
空间复杂度:O(n)
class Solution {
private boolean isPredecessor(String s1, String s2) {
int mismatch = 1;
int i = 0, j = 0;
while (i < s1.length() && j < s2.length()) {
char c1 = s1.charAt(i), c2 = s2.charAt(j);
if (c1 != c2) {
if (mismatch == 0) {
return false;
}
mismatch--;
j++;
} else {
i++;
j++;
}
}
return true;
}
public int longestStrChain(String[] words) {
Arrays.sort(words, (a, b) -> (a.length() - b.length()));
int len = words.length;
int[] dp = new int[len];
int max = 1;
for (int i = 0; i < len; i++) {
int currLen = words[i].length();
dp[i] = 1;
for (int j = i - 1; j >= 0; j--) {
if (words[j].length() == currLen) continue;
if (words[j].length() < currLen - 1) break;
if (isPredecessor(words[j], words[i])) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
max = Math.max(max, dp[i]);
}
return max;
}
}
解法二:DP + HashMap
时间复杂度:O(n log n + n * l^2), n=words.length, l=words[i].length()
空间复杂度:O(n)
class Solution {
public int longestStrChain(String[] words) {
Arrays.sort(words, (a, b) -> (a.length() - b.length()));
Map<String, Integer> dp = new HashMap<>();
dp.put(words[0], 1);
int len = words.length;
int max = 1;
for (String s : words) {
int currMax = 1;
for (int i = 0; i < s.length(); i++) {
String pre = s.substring(0, i) + s.substring(i + 1);
if (!dp.containsKey(pre)) continue;
currMax = Math.max(currMax, dp.get(pre) + 1);
}
dp.put(s, currMax);
max = Math.max(max, currMax);
}
return max;
}
}