当前位置:网站首页>522. Longest special sequence II
522. Longest special sequence II
2022-07-01 02:09:00 【Bohr651】
522. The longest special sequence II
subject
Given string list
strs, Return to it The longest special sequence The length of . 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 ).
sOf Subsequence You can delete the stringsImplementation 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 1:
Input : strs = ["aba","cdc","eae"] Output : 3Example 2:
Input : strs = ["aaa","aaa","aa"] Output : -1Tips :
2 <= strs.length <= 501 <= strs[i].length <= 10strs[i]Only lowercase lettersRelated Topics
Array
Hashtable
Double pointer
character string
Sort
Answer key
when between complex miscellaneous degree : O ( n 2 ⋅ l ) , Its in n yes Count Group strs Of Long degree , l yes word operator strand Of flat all Long degree . Time complexity :O(n^2 \cdot l), among n It's an array \textit{strs} The length of ,l Is the average length of the string . when between complex miscellaneous degree :O(n2⋅l), Its in n yes Count Group strs Of Long degree ,l yes word operator strand Of flat all Long degree .
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();
}
}
Solution ideas
The granularity of the operation is str[i] Instead of str[i] The subsequences of
- If str[i] A certain subsequence of the question is “ Special sequence ”, Add characters to it , So that the original string str[i], It shall also meet the requirements . Compared with str[i] Because the string length is longer , And win .
- So just compare each str[i], Whether it is other str[j] Subsequence of
- If more than one strp[i] accord with “ Special sequence ” requirement , Compare and take the longer one as the result
Application of double pointer
Compare strings in pairs
for(int i = 0;i < n;i++){ // Every string from beginning to end :str[i] boolean check = true; // By default, it conforms to “ Special sequence ” requirement for(int j = 0;j < n;j++){ // Then traverse to get “ the other one ” character string :str[j] if(i != j && isSubseq(strs[i],strs[j])){ // If they are different and have a substring relationship check = false; // determine str[i] Not meeting the conditions break; } } if(check) // If the conditions are met , Compare and take the longest as the result ans = Math.max(ans, strs[i].length()); }Compare whether two strings have a substring relationship
public boolean isSubseq(String s,String t){ // Judge s Is it t The string of int ptS = 0,ptT = 0; // Point to the beginning of two strings respectively while (ptS < s.length() && ptT < t.length()){ // Stop after traversing any string if(s.charAt(ptS) == t.charAt(ptT)) // The same thing s++, t++ ptS++; ptT++; // The difference is t++ } return ptS == s.length(); // If it is a substring , You should complete the traversal s }
边栏推荐
- 运算符重载的初识
- In the fourth week of June, the list - flying melon data up main growth ranking list (BiliBili platform) was released!
- Check the disk usage of MySQL database
- 删除重复的电子邮箱
- SQL语句关联表 如何添加关联表的条件 [需要null值或不需要null值]
- How to maintain efficient collaboration in remote office and achieve stable growth of projects | community essay solicitation
- Rocketqa: cross batch negatives, de noised hard negative sampling and data augmentation
- AS400 大厂面试
- go导入自建包
- Ernie gram, an explicit and complete n-gram mask language model, implements explicit n-gram semantic unit knowledge modeling.
猜你喜欢

The whole process of AS400 API from zero to one
![[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大](/img/fa/f9bad44147ba9af21183b7bd630e32.png)
[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大
![Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]

Int and bit group turn to each other

Calculate special bonus
2022年最新csdn涨薪技术栈-app自动化测试概述

halcon变量窗口的图像变量不显示,重启软件和电脑都没用

Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again

RocketQA:通过跨批次负采样(cross-batch negatives)、去噪的强负例采样(denoised hard negative sampling)与数据增强(data augment

删除重复的电子邮箱
随机推荐
P6773 [noi2020] destiny (DP, segment tree merging)
AS400 API 从零到一的整个历程
What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss
(translation) use eyebrow shaped text to improve Title click through rate
Is there any discount for opening an account now? In addition, is it safe to open a mobile account?
软件开发中的上游和下游
哪有什么未来可期,不过是打工人临死前最后的幻想罢了
SWT/ANR问题--StorageManagerService卡住
Int and bit group turn to each other
Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
SWT/ANR问题--ANR/JE引发SWT
[content of content type request header]
MySQL insert \ pre update + judgment condition
@The difference between configurationproperties and @value
正向代理和反向代理快速理解
先写API文档还是先写代码?
Template: globally balanced binary tree
With regard to the white box test, you have to master these skills~
PMP是什麼?
org. redisson. client. Redisresponsetimeoutexception: redis server response timeout (3000 ms) error resolution