当前位置:网站首页>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 <= 50
1 <= strs[i].length <= 10
strs[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 }
边栏推荐
- How to select securities companies? In addition, is it safe to open a mobile account?
- 3500字归纳总结:一名合格的软件测试工程师需要掌握的技能大全
- AS400 大厂面试
- 聚焦绿色低碳,数据中心散热进入“智能冷却”新时代
- 直播商城源码,实现左右联动商品分类页面
- New opportunities for vr/ar brought by metauniverse
- C#生成putty格式的ppk文件(支持passphrase)
- 【agora】用户管理
- [Office PDF] PDF merging and splitting will free us from the functional limitations of paid software, OK
- PHP数组拼接MySQL的in语句
猜你喜欢
迪赛智慧数——其他图表(平行坐标图):2021年应届专业就业情况
短信在企业中的应用有哪些?
FL Studio20.9水果软件高级中文版电音编曲
electron之坑addon
【JS】【掘金】获取关注了里不在关注者里的人
3500 word summary: a complete set of skills that a qualified software testing engineer needs to master
求两个线段公共部分的长度
Gin configuration file
聚焦绿色低碳,数据中心散热进入“智能冷却”新时代
For the sustainable development of software testing, we must learn to knock code?
随机推荐
一站式洞察行业热点,飞瓜数据B站新功能「流量大盘」上线!
正向代理和反向代理快速理解
What are the applications of SMS in enterprises?
Creating ASCII art with C #
农产品换房?“变相”购房补贴!
QT web development - VIDEO - Notes
数据探索电商平台用户行为流失分析
那些一门心思研究自动化测试的人,后来怎样了?
【agora】用户管理
3500 word summary: a complete set of skills that a qualified software testing engineer needs to master
7-2 拼题A打卡奖励 dp
Sun Yuchen told Swiss media Bilan that the bear market will not last long
[content of content type request header]
Lecun, a Turing Award winner, pointed out that the future of AI lies in self-learning, and the company has embarked on the journey
zabbix如何配置告警短信?(预警短信通知设置流程)
When facing the industrial Internet, they even use the ways and methods of consuming the Internet to land and practice the industrial Internet
Sort custom function
【Proteus仿真】Arduino UNO +74C922键盘解码驱动4X4矩阵键盘
URL和URI
静态域与静态方法