-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestStringChain.py
More file actions
30 lines (27 loc) · 841 Bytes
/
longestStringChain.py
File metadata and controls
30 lines (27 loc) · 841 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/longest-string-chain/
# Author: Miao Zhang
# Date: 2021-04-06
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda x: len(x))
n = len(words)
dp = [1 for _ in range(n)]
for i in range(n):
for j in range(i):
if self.valid(words[j], words[i]):
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
def valid(self, a: str, b: str) -> bool:
if len(a) + 1 != len(b): return False
cnt = 0
i, j = 0, 0
while i < len(a) and j < len(b):
if a[i] == b[j]:
i += 1
j += 1
else:
cnt += 1
j += 1
return cnt <= 1