leetcode 1048. Longest String Chain(python)
持續創作,加速成長!這是我參與「掘金日新計劃 · 6 月更文挑戰」的第34天,點選檢視活動詳情
描述
You are given an array of words where each word consists of lowercase English letters. wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB.
- For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".
A word chain is a sequence of words [word1, word2, ..., wordk] with k >= 1, where word1 is a predecessor of word2, word2 is a predecessor of word3, and so on. A single word is trivially a word chain with k == 1. Return the length of the longest possible word chain with words chosen from the given list of words.
Example 1:
Input: words = ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: One of the longest word chains is ["a","ba","bda","bdca"].
Example 2:
Input: words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
Output: 5
Explanation: All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].
Example 3:
Input: words = ["abcd","dbqca"]
Output: 1
Explanation: The trivial word chain ["abcd"] is one of the longest word chains.
["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of lowercase English letters.
解析
根據題意,給定一個單詞陣列,其中每個單詞由小寫英文字母組成。
wordA 是 wordB 的前身當且僅當我們可以在 wordA 的任意位置準確插入一個字母而不改變其他字元的順序以使其等於 wordB。
- 例如,“abc”是“abac”的前身,而“cba”不是“bcad”的前身。
一個詞鏈是一個詞序列 [word1, word2, ..., wordk] ,其中 k >= 1,其中 word1 是 word2 的前身,word2 是 word3 的前身,以此類推。返回從給定單詞列表中選擇的單詞的最長可能單詞鏈的長度。
這是一個典型的動態規劃題目,因為這條鏈有長度限制,所以我們先將 words 按照順序進行升序排序,這樣我們就能在長度限制的情況下,使用兩重迴圈挨個找子陣列能構成的最長的單詞鏈長度。我們定義一個一維陣列 dp ,dp[i] 表示的是以索引 i 單詞為末尾的單詞鏈最長的長度,如果兩個單詞 words[j] 和 words[i] 符合題意,那麼就更新 dp[i]= max(dp[i], dp[j]+1) 。遍歷結束直接返回 dp 中的最大值即可。
時間複雜度為 O(16*N^2) ,空間複雜度為 O(N^2) 。
解答
class Solution(object):
def longestStrChain(self, words):
"""
:type words: List[str]
:rtype: int
"""
def check(s1, s2):
M, N = len(s1), len(s2)
if M + 1 != N:
return False
i, j = 0, 0
while i < M and j < N:
if s1[i] == s2[j]: i += 1
j += 1
return i == M
words.sort(key=lambda x: (len(x), x))
N = len(words)
dp = [1] * N
for i in range(N):
for j in range(i):
if check(words[j], words[i]):
dp[i] = max(dp[i], dp[j]+1)
return max(dp)
執行結果
Runtime: 3021 ms, faster than 5.56% of Python online submissions for Longest String Chain.
Memory Usage: 14.2 MB, less than 24.50% of Python online submissions for Longest String Chain.
原題連結
https://leetcode.com/problems/longest-string-chain/
您的支援是我最大的動力
- linux 安裝 ES 8.0 和 kibana 8.0 爬米共坑
- ES 8.0 及其生態配置過程一條龍服務
- 2022 年中紀
- leetcode 1048. Longest String Chain(python)
- leetcode 1354. Construct Target Array With Multiple Sums(python)
- leetcode 2311. Longest Binary Subsequence Less Than or Equal to K(python)
- leetcode 2304. Minimum Path Cost in a Grid(python)
- leetcode 2303. Calculate Amount Paid in Taxes(python)
- 帶你輕鬆入門 Bert
- leetcode 1332. Remove Palindromic Subsequences(python)
- leetcode 3. Longest Substring Without Repeating Characters (python)
- leetcode 2296. Design a Text Editor (python)
- leetcode 2293. Min Max Game(python)
- leetcode 1584. Min Cost to Connect All Points(python)
- leetcode 318. Maximum Product of Word Lengths(python)
- 沙灘 美女才是夏天的正確開啟方式
- leetcode 2275. Largest Combination With Bitwise AND Greater Than Zero (python)
- leetcode 2258. Escape the Spreading Fire(python)
- leetcode 2257. Count Unguarded Cells in the Grid(python)
- leetcode 2273. Find Resultant Array After Removing Anagrams(python)