Skip to content

Commit 0d6fda1

Browse files
committedAug 12, 2021
feat: add solutions to lc problem:No.1048.Longest String Chain
1 parent 8b6af50 commit 0d6fda1

File tree

6 files changed

+297
-9
lines changed

6 files changed

+297
-9
lines changed
 

Diff for: ‎solution/1000-1099/1048.Longest String Chain/README.md

+120-1
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,146 @@
3434

3535
<p>&nbsp;</p>
3636

37-
3837
## 解法
3938

4039
<!-- 这里可写通用的实现逻辑 -->
4140

41+
先按字符串长度升序排列,再利用动态规划或者哈希表求解。
42+
4243
<!-- tabs:start -->
4344

4445
### **Python3**
4546

4647
<!-- 这里可写当前语言的特殊实现逻辑 -->
4748

49+
动态规划:
50+
4851
```python
52+
class Solution:
53+
def longestStrChain(self, words: List[str]) -> int:
54+
def check(w1, w2):
55+
if len(w2) - len(w1) != 1:
56+
return False
57+
i = j = cnt = 0
58+
while i < len(w1) and j < len(w2):
59+
if w1[i] != w2[j]:
60+
cnt += 1
61+
else:
62+
i += 1
63+
j += 1
64+
return cnt < 2 and i == len(w1)
65+
66+
n = len(words)
67+
dp = [1] * (n + 1)
68+
words.sort(key= lambda x: len(x))
69+
res = 1
70+
for i in range(1, n):
71+
for j in range(i):
72+
if check(words[j], words[i]):
73+
dp[i] = max(dp[i], dp[j] + 1)
74+
res = max(res, dp[i])
75+
return res
76+
```
77+
78+
哈希表:
4979

80+
```python
81+
class Solution:
82+
def longestStrChain(self, words: List[str]) -> int:
83+
words.sort(key= lambda x: len(x))
84+
res = 0
85+
mp = {}
86+
for word in words:
87+
x = 1
88+
for i in range(len(word)):
89+
pre = word[:i] + word[i + 1:]
90+
x = max(x, mp.get(pre, 0) + 1)
91+
mp[word] = x
92+
res = max(res, x)
93+
return res
5094
```
5195

5296
### **Java**
5397

5498
<!-- 这里可写当前语言的特殊实现逻辑 -->
5599

100+
哈希表:
101+
56102
```java
103+
class Solution {
104+
public int longestStrChain(String[] words) {
105+
Arrays.sort(words, Comparator.comparingInt(String::length));
106+
int res = 0;
107+
Map<String, Integer> map = new HashMap<>();
108+
for (String word : words) {
109+
int x = 1;
110+
for (int i = 0; i < word.length(); ++i) {
111+
String pre = word.substring(0, i) + word.substring(i + 1);
112+
x = Math.max(x, map.getOrDefault(pre, 0) + 1);
113+
}
114+
map.put(word, x);
115+
res = Math.max(res, x);
116+
}
117+
return res;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
哈希表:
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
int longestStrChain(vector<string> &words) {
130+
sort(words.begin(), words.end(), [&](string a, string b)
131+
{ return a.size() < b.size(); });
132+
int res = 0;
133+
unordered_map<string, int> map;
134+
for (auto word : words)
135+
{
136+
int x = 1;
137+
for (int i = 0; i < word.size(); ++i)
138+
{
139+
string pre = word.substr(0, i) + word.substr(i + 1);
140+
x = max(x, map[pre] + 1);
141+
}
142+
map[word] = x;
143+
res = max(res, x);
144+
}
145+
return res;
146+
}
147+
};
148+
```
57149
150+
### **Go**
151+
152+
哈希表:
153+
154+
```go
155+
func longestStrChain(words []string) int {
156+
sort.Slice(words, func(i, j int) bool { return len(words[i]) < len(words[j]) })
157+
res := 0
158+
mp := make(map[string]int)
159+
for _, word := range words {
160+
x := 1
161+
for i := 0; i < len(word); i++ {
162+
pre := word[0:i] + word[i+1:len(word)]
163+
x = max(x, mp[pre]+1)
164+
}
165+
mp[word] = x
166+
res = max(res, x)
167+
}
168+
return res
169+
}
170+
171+
func max(a, b int) int {
172+
if a > b {
173+
return a
174+
}
175+
return b
176+
}
58177
```
59178

60179
### **...**

Diff for: ‎solution/1000-1099/1048.Longest String Chain/README_EN.md

+108-1
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,128 @@
3737
<li><code>words[i]</code> only consists of English lowercase letters.</li>
3838
</ul>
3939

40-
4140
## Solutions
4241

4342
<!-- tabs:start -->
4443

4544
### **Python3**
4645

4746
```python
47+
class Solution:
48+
def longestStrChain(self, words: List[str]) -> int:
49+
def check(w1, w2):
50+
if len(w2) - len(w1) != 1:
51+
return False
52+
i = j = cnt = 0
53+
while i < len(w1) and j < len(w2):
54+
if w1[i] != w2[j]:
55+
cnt += 1
56+
else:
57+
i += 1
58+
j += 1
59+
return cnt < 2 and i == len(w1)
60+
61+
n = len(words)
62+
dp = [1] * (n + 1)
63+
words.sort(key= lambda x: len(x))
64+
res = 1
65+
for i in range(1, n):
66+
for j in range(i):
67+
if check(words[j], words[i]):
68+
dp[i] = max(dp[i], dp[j] + 1)
69+
res = max(res, dp[i])
70+
return res
71+
```
4872

73+
```python
74+
class Solution:
75+
def longestStrChain(self, words: List[str]) -> int:
76+
words.sort(key= lambda x: len(x))
77+
res = 0
78+
mp = {}
79+
for word in words:
80+
x = 1
81+
for i in range(len(word)):
82+
pre = word[:i] + word[i + 1:]
83+
x = max(x, mp.get(pre, 0) + 1)
84+
mp[word] = x
85+
res = max(res, x)
86+
return res
4987
```
5088

5189
### **Java**
5290

5391
```java
92+
class Solution {
93+
public int longestStrChain(String[] words) {
94+
Arrays.sort(words, Comparator.comparingInt(String::length));
95+
int res = 0;
96+
Map<String, Integer> map = new HashMap<>();
97+
for (String word : words) {
98+
int x = 1;
99+
for (int i = 0; i < word.length(); ++i) {
100+
String pre = word.substring(0, i) + word.substring(i + 1);
101+
x = Math.max(x, map.getOrDefault(pre, 0) + 1);
102+
}
103+
map.put(word, x);
104+
res = Math.max(res, x);
105+
}
106+
return res;
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int longestStrChain(vector<string> &words) {
117+
sort(words.begin(), words.end(), [&](string a, string b)
118+
{ return a.size() < b.size(); });
119+
int res = 0;
120+
unordered_map<string, int> map;
121+
for (auto word : words)
122+
{
123+
int x = 1;
124+
for (int i = 0; i < word.size(); ++i)
125+
{
126+
string pre = word.substr(0, i) + word.substr(i + 1);
127+
x = max(x, map[pre] + 1);
128+
}
129+
map[word] = x;
130+
res = max(res, x);
131+
}
132+
return res;
133+
}
134+
};
135+
```
54136
137+
### **Go**
138+
139+
```go
140+
func longestStrChain(words []string) int {
141+
sort.Slice(words, func(i, j int) bool { return len(words[i]) < len(words[j]) })
142+
res := 0
143+
mp := make(map[string]int)
144+
for _, word := range words {
145+
x := 1
146+
for i := 0; i < len(word); i++ {
147+
pre := word[0:i] + word[i+1:len(word)]
148+
x = max(x, mp[pre]+1)
149+
}
150+
mp[word] = x
151+
res = max(res, x)
152+
}
153+
return res
154+
}
155+
156+
func max(a, b int) int {
157+
if a > b {
158+
return a
159+
}
160+
return b
161+
}
55162
```
56163

57164
### **...**
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int longestStrChain(vector<string> &words) {
4+
sort(words.begin(), words.end(), [&](string a, string b)
5+
{ return a.size() < b.size(); });
6+
int res = 0;
7+
unordered_map<string, int> map;
8+
for (auto word : words)
9+
{
10+
int x = 1;
11+
for (int i = 0; i < word.size(); ++i)
12+
{
13+
string pre = word.substr(0, i) + word.substr(i + 1);
14+
x = max(x, map[pre] + 1);
15+
}
16+
map[word] = x;
17+
res = max(res, x);
18+
}
19+
return res;
20+
}
21+
};
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func longestStrChain(words []string) int {
2+
sort.Slice(words, func(i, j int) bool { return len(words[i]) < len(words[j]) })
3+
res := 0
4+
mp := make(map[string]int)
5+
for _, word := range words {
6+
x := 1
7+
for i := 0; i < len(word); i++ {
8+
pre := word[0:i] + word[i+1:len(word)]
9+
x = max(x, mp[pre]+1)
10+
}
11+
mp[word] = x
12+
res = max(res, x)
13+
}
14+
return res
15+
}
16+
17+
func max(a, b int) int {
18+
if a > b {
19+
return a
20+
}
21+
return b
22+
}
+2-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
class Solution {
22
public int longestStrChain(String[] words) {
3-
Arrays.sort(words, new Comparator<String>() {
4-
@Override
5-
public int compare(String o1, String o2) {
6-
return Integer.compare(o1.length(), o2.length());
7-
}
8-
});
3+
Arrays.sort(words, Comparator.comparingInt(String::length));
94
int res = 0;
105
Map<String, Integer> map = new HashMap<>();
116
for (String word : words) {
@@ -19,4 +14,4 @@ public int compare(String o1, String o2) {
1914
}
2015
return res;
2116
}
22-
}
17+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def longestStrChain(self, words: List[str]) -> int:
3+
def check(w1, w2):
4+
if len(w2) - len(w1) != 1:
5+
return False
6+
i = j = cnt = 0
7+
while i < len(w1) and j < len(w2):
8+
if w1[i] != w2[j]:
9+
cnt += 1
10+
else:
11+
i += 1
12+
j += 1
13+
return cnt < 2 and i == len(w1)
14+
15+
n = len(words)
16+
dp = [1] * (n + 1)
17+
words.sort(key=lambda x: len(x))
18+
res = 1
19+
for i in range(1, n):
20+
for j in range(i):
21+
if check(words[j], words[i]):
22+
dp[i] = max(dp[i], dp[j] + 1)
23+
res = max(res, dp[i])
24+
return res

0 commit comments

Comments
 (0)
Please sign in to comment.