当前位置:网站首页>522. 最长的特殊序列 II
522. 最长的特殊序列 II
2022-07-01 01:20:00 【Bohr651】
522. 最长的特殊序列 II
题目
给定字符串列表
strs,返回其中 最长的特殊序列 的长度。如果最长特殊序列不存在,返回-1。特殊序列 定义如下:该序列为某字符串 独有的子序列(即不能是其他字符串的子序列)。
s的 子序列可以通过删去字符串s中的某些字符实现。
- 例如,
"abc"是"aebdc"的子序列,因为您可以删除"aebdc"中的下划线字符来得到"abc"。"aebdc"的子序列还包括"aebdc"、"aeb"和 “” (空字符串)。示例 1:
输入: strs = ["aba","cdc","eae"] 输出: 3示例 2:
输入: strs = ["aaa","aaa","aa"] 输出: -1提示:
2 <= strs.length <= 501 <= strs[i].length <= 10strs[i]只包含小写英文字母Related Topics
数组
哈希表
双指针
字符串
排序
题解
时 间 复 杂 度 : O ( n 2 ⋅ l ) , 其 中 n 是 数 组 strs 的 长 度 , l 是 字 符 串 的 平 均 长 度 。 时间复杂度:O(n^2 \cdot l),其中 n 是数组 \textit{strs} 的长度,l 是字符串的平均长度。 时间复杂度:O(n2⋅l),其中n是数组strs的长度,l是字符串的平均长度。
class Solution {
public int findLUSlength(String[] strs) {
int n = strs.length;
int ans = -1;
for(int i = 0;i < n;i++){
boolean check = true;
for(int j = 0;j < n;j++){
if(i != j && isSubseq(strs[i],strs[j])){
check = false;
break;
}
}
if(check)
ans = Math.max(ans, strs[i].length());
}
return ans;
}
public boolean isSubseq(String s,String t){
int ptS = 0,ptT = 0;
while (ptS < s.length() && ptT < t.length()){
if(s.charAt(ptS) == t.charAt(ptT))
ptS++;
ptT++;
}
return ptS == s.length();
}
}
解法思路
操作细粒度为 str[i] 而非 str[i]的各子序列
- 如果str[i]的某子序列为符合题目要求的“特殊序列”,则对其添加字符,以至于成原字符串str[i],也应满足要求。相比而言str[i]因字符串长度更长,而优胜。
- 所以只需比较各str[i],是否为其他str[j]的子序列即可
- 若多个strp[i]符合“特殊序列”要求,比较取更长者为结果
双指针的应用
两两比较字符串
for(int i = 0;i < n;i++){ //从头到尾的每个字符串:str[i] boolean check = true; //默认它是符合“特殊序列”要求 for(int j = 0;j < n;j++){ //再遍历取得“另一个”字符串:str[j] if(i != j && isSubseq(strs[i],strs[j])){ //若两者不同且有子串关系 check = false; //判定str[i]不满足条件 break; } } if(check) //若满足条件,比较取最长为结果 ans = Math.max(ans, strs[i].length()); }比较两字符串是否有子串关系
public boolean isSubseq(String s,String t){ //判断 s 是否为 t 的子串 int ptS = 0,ptT = 0; //分别指向两字符串首 while (ptS < s.length() && ptT < t.length()){ //任一字符串遍历完即停止 if(s.charAt(ptS) == t.charAt(ptT)) //相同则 s++, t++ ptS++; ptT++; //不同则 t++ } return ptS == s.length(); //若为子串,则应遍历完 s }
边栏推荐
- QT web 开发 - video -- 笔记
- Sun Yuchen told Swiss media Bilan that the bear market will not last long
- Handsontable数据网格组件
- zabbix如何配置告警短信?(预警短信通知设置流程)
- With regard to the white box test, you have to master these skills~
- 数据探索电商平台用户行为流失分析
- org.redisson.client.RedisResponseTimeoutException: Redis server response timeout (3000 ms)错误解决
- (翻译)实时内联验证更容易让用户犯错的原因
- Microbiological health, why is food microbiological testing important
- laravel+redis 生成订单号-当天从1开始自增
猜你喜欢

zabbix如何配置告警短信?(预警短信通知设置流程)

The personal test is effective, and the JMeter desktop shortcut is quickly created

How does ZABBIX configure alarm SMS? (alert SMS notification setting process)

Qt5 mvc: revealing the secrets of data visualization
![[无线通信基础-15]:图解移动通信技术与应用发展-3- 数字通信2G GSM、CDMA、3G WDCMA/CDMA200/TD-SCDMA、4G LTE、5G NR概述](/img/22/1efa444220131359b06005f597c9db.png)
[无线通信基础-15]:图解移动通信技术与应用发展-3- 数字通信2G GSM、CDMA、3G WDCMA/CDMA200/TD-SCDMA、4G LTE、5G NR概述

工作6年,来盘点一下职场人混迹职场的黄金法则

微生物健康,食品微生物检测为什么很重要

PHP通过第三方插件爬取数据

gin_ gorm

KS009基于SSH实现宠物管理系统
随机推荐
如何学习和阅读代码
(翻译)实时内联验证更容易让用户犯错的原因
Handsontable數據網格組件
P6773 [NOI2020] 命运(dp、线段树合并)
数学知识:求组合数 IV—求组合数
[dynamic planning] path dp:931 Minimum Falling Path Sum
工作八年的程序员,却拿着毕业三年的工资,再不开窍就真晚了...
Institute of Microbiology, commonly used biochemical reactions in microbiological testing
What will Web3 bring in the future?
股票开户有哪些优惠活动?另外,手机开户安全么?
工作6年,来盘点一下职场人混迹职场的黄金法则
Laravel event & Monitoring
软件开发中的上游和下游
Some items of OCR
小程序中实现excel数据的批量导入
New opportunities for vr/ar brought by metauniverse
gin_ gorm
Selenium classic interview question - multi window switching solution
Analysis on user behavior loss of data exploration e-commerce platform
Lecun, a Turing Award winner, pointed out that the future of AI lies in self-learning, and the company has embarked on the journey