当前位置:网站首页>【522. 最长特殊序列 II】
【522. 最长特殊序列 II】
2022-06-28 01:22:00 【Sugar_wolf】
来源:力扣(LeetCode)
描述:
给定字符串列表 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] 只包含小写英文字母
方法 :枚举每个字符串
思路与算法
对于给定的某个字符串 str[i],如果它的一个子序列 sub 是 「特殊序列」,那么 str[i] 本身也是一个 「特殊序列」。
这是因为如果 sub 如果没有在除了 str[i] 之外的字符串中以子序列的形式出现过,那么给 sub 不断地添加字符,它也不会出现。而 str[i] 就是 sub 添加若干个(可以为零个)字符得到的结果。
因此我们只需要使用一个双重循环,外层枚举每一个字符串 str[i] 作为特殊序列,内层枚举每一个字符串 str[j] (i != j),判断 str[i] 是否不为 str[j] 的子序列即可。
要想判断 str[i] 是否为 str[j] 的子序列,我们可以使用 贪心 + 双指针 的方法:即初始时指针 pti 和 ptj分别指向两个字符串的首字符。如果两个字符相同,那么两个指针都往右移动一个位置,表示匹配成功;否则只往右移动指针 ptj,表示匹配失败。如果 pti遍历完了整个字符串,就说明 str[i] 是 str[j] 的子序列。
在所有满足要求的 str[i] 中,我们选出最长的那个,返回其长度作为答案。如果不存在满足要求的字符串,那么返回 −1。
代码:
class Solution {
public:
int findLUSlength(vector<string>& strs) {
auto is_subseq = [](const string& s, const string& t) -> bool {
int pt_s = 0, pt_t = 0;
while (pt_s < s.size() && pt_t < t.size()) {
if (s[pt_s] == t[pt_t]) {
++pt_s;
}
++pt_t;
}
return pt_s == s.size();
};
int n = strs.size();
int ans = -1;
for (int i = 0; i < n; ++i) {
bool check = true;
for (int j = 0; j < n; ++j) {
if (i != j && is_subseq(strs[i], strs[j])) {
check = false;
break;
}
}
if (check) {
ans = max(ans, static_cast<int>(strs[i].size()));
}
}
return ans;
}
};
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:8.1 MB, 在所有 C++ 提交中击败了67.00%的用户
复杂度分析
时间复杂度:O(n 2 * l),其中 n 是数组 strs 的长度, l 是字符串的平均长度。
空间复杂度: O(1)。
author:LeetCode-Solution
边栏推荐
- Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
- Basic flask: template rendering + template filtering + control statement
- Arduino Esp8266 Web LED控制
- 面试:List 如何根据对象的属性去重?
- Initial linear regression
- [today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper
- ByteDance Interviewer: how to calculate the memory size occupied by a picture
- Single page application (SPA) hash route and historical API route
- 《天天数学》连载53:二月二十一日
- Digital intelligence learning Lake Warehouse Integration Practice and exploration
猜你喜欢

Exploration on the construction path of real-time digital warehouse integrating digital intelligence learning and streaming batch
![[today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out](/img/1b/31b5adbec5182207c371a23e41baa3.png)
[today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out

榜单首发——前装搭载率站上10%大关,数字钥匙方案供应商TOP10
![[today in history] June 2: Apple launched swift programming language; China Telecom acquires China Unicom C network; OS X Yosemite release](/img/24/58c4ee72e067f01a4c4aa57a1cf61a.jpg)
[today in history] June 2: Apple launched swift programming language; China Telecom acquires China Unicom C network; OS X Yosemite release

Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
![[cloud native] - docker installation and deployment of distributed database oceanbase](/img/02/57ab785acafd8ff0a49c584dd05188.png)
[cloud native] - docker installation and deployment of distributed database oceanbase

【插件-statistic】统计代码行数和相关数据
![[postgraduate] bit by bit](/img/76/b804ff215b8f52f1fe603a9a34a352.jpg)
[postgraduate] bit by bit
![[today in history] June 3: Microsoft launched Bing search engine; Larry Roberts starts ARPANET; The father of Visual Basic was born](/img/9e/408ae022d9d5d8106e94e71b319d43.png)
[today in history] June 3: Microsoft launched Bing search engine; Larry Roberts starts ARPANET; The father of Visual Basic was born

Arduino Esp8266 Web LED控制
随机推荐
Win11 ne peut pas faire glisser l'image sur le logiciel de la barre des tâches
元宇宙标准论坛成立
Arduino esp8266 web LED control
Reprinted article: the digital economy generates strong demand for computing power Intel releases a number of innovative technologies to tap the potential of computing power
Is it reliable to invest in the inter-bank certificate of deposit fund? Is the inter-bank certificate of deposit fund safe
字节跳动面试官:一张图片占据的内存大小是如何计算
买股票应该下载什么软件最好最安全?
Review the submission of small papers for 2022 spring semester courses
The first place on the list - the carrying rate of front-end equipment is up to 10%, and the top 10 suppliers of digital key solutions
Online text batch inversion by line tool
分布式事务—基于消息补偿的最终一致性方案(本地消息表、消息队列)
Feign远程调用fallback回调失败,无效果
Usage differences between isempty and isblank
Win11无法使用动态壁纸怎么办?Win11用不了动态壁纸的解决方法
【倒立摆控制】基于UKF无迹卡尔曼滤波的倒立摆控制simulink仿真
[inverted pendulum control] Simulink simulation of inverted pendulum control based on UKF unscented Kalman filter
Interview: how do lists duplicate objects according to their attributes?
【活动早知道】LiveVideoStack近期活动一览
【二維碼圖像矯正增强】基於MATLAB的二維碼圖像矯正增强處理仿真
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times