当前位置:网站首页>Leetcode daily question - 522 Longest special sequence II
Leetcode daily question - 522 Longest special sequence II
2022-06-28 20:40:00 【Did HYK write the algorithm today】
List of articles
subject
Given string list strs , Return to it The longest special sequence . If the longest special sequence does not exist , return -1 .
Special sequence The definition is as follows : The sequence is a string Unique subsequence ( That is, it cannot be a subsequence of another string ).
s Of Subsequences can be made by deleting strings s Implementation of some characters in .
- for example ,“abc” yes “aebdc” The subsequence , Because you can delete "aebdc" Use the underscore character in to get “abc”
.“aebdc" The subsequence of also includes "aebdc”、 “aeb” and “” ( An empty string ).
Example
Example 1:
Input : strs = [“aba”,“cdc”,“eae”]
Output : 3
Example 2:
Input : strs = [“aaa”,“aaa”,“aa”]
Output : -1
Tips :
2 <= strs.length <= 50
1 <= strs[i].length <= 10
strs[i] Only lowercase letters
Ideas
- Meaning of this question : Find the length of the longest repeatless subsequence
- Here's a way of thinking : That is, the subsequence of a string is a non repeating subsequence , The string itself is also a non repeating subsequence ( perhaps : If this string is a subsequence of other strings , Then all subsequences of this string are subsequences of other strings , There is no need to compare subsequences of this string )
- According to this idea , Just judge whether this string is a subsequence of other strings , If this string is not a subsequence of all other strings , Record length , Finally, find the maximum length
Answer key
class Solution:
def findLUSlength(self, strs: List[str]) -> int:
# Determine whether it is a subsequence
def isson(str1, str2):
i,j = 0, 0
while i < len(str1) and j < len(str2):
if str1[i] == str2[j]:
i += 1
j += 1
else:
j += 1
return i == len(str1)
ans = -1
for i in range(len(strs)):
judge = True
for j in range(len(strs)):
if i == j:
continue
if isson(strs[i], strs[j]):
judge = False
break
# Maximum value of update results
if judge:
ans = max(ans, len(strs[i]))
return ans
边栏推荐
- Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
- Rsync remote synchronization
- Understand the construction of the entire network model
- 数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
- ANR无响应介绍
- 国产数据库名录一览
- 2022 P cylinder filling test exercises and online simulation test
- ComparisonChain-文件名排序
- 字符和整数
- 嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
猜你喜欢

Lecture 30 linear algebra Lecture 4 linear equations

Ehcache配置资料,方便自己查

28 rounds of interviews with 10 companies in two and a half years

openGauss内核分析之查询重写

Bitbucket failed to pull the warehouse Using SSH

How do I download videos? Look at the super simple method!

mysql-发生系统错误1067

UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation

Ref attribute, props configuration, mixin mixing, plug-in, scoped style

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
随机推荐
Stability summary
MongoDB——副本集与分片
How to use dataant to monitor Apache apisex
实型数运算
No module named ‘PyEMD‘ ;使用plt.figure()TypeError: ‘module‘ object is not callable
学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
Application of Andy s first dictionary (uva10815) Purple Book p112set
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
How to do a good job in customer's successful bottom design | tob Master Course
Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
输入和输出字符型数据
rapid ssl通配符证书八百一年是正版吗
字符和整数
Shell reads the value of the JSON file
SaaS sales upgrade under the new situation | tob Master Course
2. integrate filter
Comparisonchain file name sort
oracle delete误删除表数据后如何恢复
The blocks problem (uva101) Purple Book p110vector application
请允许当下国内ToB的「不完美」