Skip to content

Commit 58b2c0c

Browse files
committed
+ problem 1048
1 parent e7e1eb1 commit 58b2c0c

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 1048. Longest String Chain
2+
You are given an array of `words` where each word consists of lowercase English letters.
3+
4+
<code>word<sub>A</sub></code> is a **predecessor** of <code>word<sub>B</sub></code> if and only if we can insert **exactly one** letter anywhere in <code>word<sub>A</sub></code> **without changing the order of the other characters** to make it equal to <code>word<sub>B</sub></code>.
5+
6+
* For example, `"abc"` is a **predecessor** of `"abac"`, while `"cba"` is not a **predecessor** of `"bcad"`.
7+
8+
A **word chain** is a sequence of words <code>[word<sub>1</sub>, word<sub>2</sub>, ..., word<sub>k</sub>]</code> with `k >= 1`, where <code>word<sub>1</sub></code> is a **predecessor** of <code>word<sub>2</sub></code>, <code>word<sub>2</sub></code> is a **predecessor** of <code>word<sub>3</sub></code>, and so on. A single word is trivially a **word chain** with `k == 1`.
9+
10+
Return *the **length** of the **longest possible word chain** with words chosen from the given list of* `words`.
11+
12+
#### Example 1:
13+
<pre>
14+
<strong>Input:</strong> words = ["a","b","ba","bca","bda","bdca"]
15+
<strong>Output:</strong> 4
16+
<strong>Explanation:</strong> One of the longest word chains is ["a","ba","bda","bdca"].
17+
</pre>
18+
19+
#### Example 2:
20+
<pre>
21+
<strong>Input:</strong> words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
22+
<strong>Output:</strong> 5
23+
<strong>Explanation:</strong> All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].
24+
</pre>
25+
26+
#### Example 3:
27+
<pre>
28+
<strong>Input:</strong> words = ["abcd","dbqca"]
29+
<strong>Output:</strong> 1
30+
<strong>Explanation:</strong> The trivial word chain ["abcd"] is one of the longest word chains.
31+
["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
32+
</pre>
33+
34+
#### Constraints:
35+
* `1 <= words.length <= 1000`
36+
* `1 <= words[i].length <= 16`
37+
* `words[i]` only consists of lowercase English letters.
38+
39+
## Solutions (Python)
40+
41+
### 1. Solution
42+
```Python
43+
class Solution:
44+
def longestStrChain(self, words: List[str]) -> int:
45+
chainlen = {word: 1 for word in words}
46+
47+
for word in sorted(words, key=len):
48+
for i in range(len(word)):
49+
pred = word[:i] + word[i + 1:]
50+
chainlen[word] = max(chainlen[word], chainlen.get(pred, 0) + 1)
51+
52+
return max(chainlen.values())
53+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 1048. 最长字符串链
2+
给出一个单词数组 `words` ,其中每个单词都由小写英文字母组成。
3+
4+
如果我们可以 **不改变其他字符的顺序** ,在 <code>word<sub>A</sub></code> 的任何地方添加 **恰好一个** 字母使其变成 <code>word<sub>B</sub></code> ,那么我们认为 <code>word<sub>A</sub></code> 是 <code>word<sub>B</sub></code> 的 **前身**
5+
6+
* 例如,`"abc"``"abac"`**前身** ,而 `"cba"` 不是 `"bcad"`**前身**
7+
8+
**词链**是单词 `[word_1, word_2, ..., word_k]` 组成的序列,`k >= 1`,其中 <code>word<sub>1</sub></code> 是 <code>word<sub>2</sub></code> 的前身,<code>word<sub>2</sub></code> 是 <code>word<sub>3</sub></code> 的前身,依此类推。一个单词通常是 `k == 1`**单词链**
9+
10+
从给定单词列表 `words` 中选择单词组成词链,返回 词链的 **最长可能长度**
11+
12+
#### 示例 1:
13+
<pre>
14+
<strong>输入:</strong> words = ["a","b","ba","bca","bda","bdca"]
15+
<strong>输出:</strong> 4
16+
<strong>解释:</strong> 最长单词链之一为 ["a","ba","bda","bdca"]
17+
</pre>
18+
19+
#### 示例 2:
20+
<pre>
21+
<strong>输入:</strong> words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
22+
<strong>输出:</strong> 5
23+
<strong>解释:</strong> 所有的单词都可以放入单词链 ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].
24+
</pre>
25+
26+
#### 示例 3:
27+
<pre>
28+
<strong>输入:</strong> words = ["abcd","dbqca"]
29+
<strong>输出:</strong> 1
30+
<strong>解释:</strong> 字链["abcd"]是最长的字链之一。
31+
["abcd","dbqca"]不是一个有效的单词链,因为字母的顺序被改变了。
32+
</pre>
33+
34+
#### 提示:
35+
* `1 <= words.length <= 1000`
36+
* `1 <= words[i].length <= 16`
37+
* `words[i]` 仅由小写英文字母组成。
38+
39+
## 题解 (Python)
40+
41+
### 1. 题解
42+
```Python
43+
class Solution:
44+
def longestStrChain(self, words: List[str]) -> int:
45+
chainlen = {word: 1 for word in words}
46+
47+
for word in sorted(words, key=len):
48+
for i in range(len(word)):
49+
pred = word[:i] + word[i + 1:]
50+
chainlen[word] = max(chainlen[word], chainlen.get(pred, 0) + 1)
51+
52+
return max(chainlen.values())
53+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def longestStrChain(self, words: List[str]) -> int:
3+
chainlen = {word: 1 for word in words}
4+
5+
for word in sorted(words, key=len):
6+
for i in range(len(word)):
7+
pred = word[:i] + word[i + 1:]
8+
chainlen[word] = max(chainlen[word], chainlen.get(pred, 0) + 1)
9+
10+
return max(chainlen.values())

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@
670670
[1043][1043l]|[Partition Array for Maximum Sum][1043] |![rs]
671671
[1046][1046l]|[Last Stone Weight][1046] |![py]
672672
[1047][1047l]|[Remove All Adjacent Duplicates In String][1047] |![py]
673+
[1048][1048l]|[Longest String Chain][1048] |![py]
673674
[1051][1051l]|[Height Checker][1051] |![py]
674675
[1052][1052l]|[Grumpy Bookstore Owner][1052] |![rb]&nbsp;&nbsp;![rs]
675676
[1053][1053l]|[Previous Permutation With One Swap][1053] |![rs]
@@ -2104,6 +2105,7 @@
21042105
[1043]:Problemset/1043-Partition%20Array%20for%20Maximum%20Sum/README.md#1043-partition-array-for-maximum-sum
21052106
[1046]:Problemset/1046-Last%20Stone%20Weight/README.md#1046-last-stone-weight
21062107
[1047]:Problemset/1047-Remove%20All%20Adjacent%20Duplicates%20In%20String/README.md#1047-remove-all-adjacent-duplicates-in-string
2108+
[1048]:Problemset/1048-Longest%20String%20Chain/README.md#1048-longest-string-chain
21072109
[1051]:Problemset/1051-Height%20Checker/README.md#1051-height-checker
21082110
[1052]:Problemset/1052-Grumpy%20Bookstore%20Owner/README.md#1052-grumpy-bookstore-owner
21092111
[1053]:Problemset/1053-Previous%20Permutation%20With%20One%20Swap/README.md#1053-previous-permutation-with-one-swap
@@ -3537,6 +3539,7 @@
35373539
[1043l]:https://leetcode.com/problems/partition-array-for-maximum-sum/
35383540
[1046l]:https://leetcode.com/problems/last-stone-weight/
35393541
[1047l]:https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
3542+
[1048l]:https://leetcode.com/problems/longest-string-chain/
35403543
[1051l]:https://leetcode.com/problems/height-checker/
35413544
[1052l]:https://leetcode.com/problems/grumpy-bookstore-owner/
35423545
[1053l]:https://leetcode.com/problems/previous-permutation-with-one-swap/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@
670670
[1043][1043l]|[分隔数组以得到最大和][1043] |![rs]
671671
[1046][1046l]|[最后一块石头的重量][1046] |![py]
672672
[1047][1047l]|[删除字符串中的所有相邻重复项][1047] |![py]
673+
[1048][1048l]|[最长字符串链][1048] |![py]
673674
[1051][1051l]|[高度检查器][1051] |![py]
674675
[1052][1052l]|[爱生气的书店老板][1052] |![rb]&nbsp;&nbsp;![rs]
675676
[1053][1053l]|[交换一次的先前排列][1053] |![rs]
@@ -2104,6 +2105,7 @@
21042105
[1043]:Problemset/1043-Partition%20Array%20for%20Maximum%20Sum/README_CN.md#1043-分隔数组以得到最大和
21052106
[1046]:Problemset/1046-Last%20Stone%20Weight/README_CN.md#1046-最后一块石头的重量
21062107
[1047]:Problemset/1047-Remove%20All%20Adjacent%20Duplicates%20In%20String/README_CN.md#1047-删除字符串中的所有相邻重复项
2108+
[1048]:Problemset/1048-Longest%20String%20Chain/README_CN.md#1048-最长字符串链
21072109
[1051]:Problemset/1051-Height%20Checker/README_CN.md#1051-高度检查器
21082110
[1052]:Problemset/1052-Grumpy%20Bookstore%20Owner/README_CN.md#1052-爱生气的书店老板
21092111
[1053]:Problemset/1053-Previous%20Permutation%20With%20One%20Swap/README_CN.md#1053-交换一次的先前排列
@@ -3537,6 +3539,7 @@
35373539
[1043l]:https://leetcode.cn/problems/partition-array-for-maximum-sum/
35383540
[1046l]:https://leetcode.cn/problems/last-stone-weight/
35393541
[1047l]:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
3542+
[1048l]:https://leetcode.cn/problems/longest-string-chain/
35403543
[1051l]:https://leetcode.cn/problems/height-checker/
35413544
[1052l]:https://leetcode.cn/problems/grumpy-bookstore-owner/
35423545
[1053l]:https://leetcode.cn/problems/previous-permutation-with-one-swap/

0 commit comments

Comments
 (0)