当前位置:网站首页>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 }
边栏推荐
- Delete duplicate email
- 【JS给元素添加属性:setAttribute;classList.remove;classList.add;】
- 端口号和进程号的区别?
- [punch in questions] integrated daily 5 questions sharing (phase I)
- 数学知识:求组合数 IV—求组合数
- 数据探索电商平台用户行为流失分析
- Upstream and downstream in software development
- How to learn and read code
- Factory + strategy mode
- Check the disk usage of MySQL database
猜你喜欢

CorelDRAW 2022中文精简64位直装版下载

Delete duplicate email

7-2 拼题A打卡奖励 dp

计算特殊奖金

正向代理和反向代理快速理解

数据探索电商平台用户行为流失分析

AS400 API 从零到一的整个历程

The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer

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

项目管理是什么?
随机推荐
What are the preferential activities for stock account opening? In addition, is it safe to open a mobile account?
Electron pit Addon
How to maintain efficient collaboration in remote office and achieve stable growth of projects | community essay solicitation
SWT/ANR问题--Binder Stuck
SWT / anr problem - SWT caused by long execution time of native method
机器学习10-信念贝叶斯分类器
[JS adds attributes to elements: setAttribute; classlist.remove; classlist.add;]
Do you write API documents or code first?
MySQL insert \ pre update + judgment condition
先写API文档还是先写代码?
How to select securities companies? In addition, is it safe to open a mobile account?
In the fourth week of June, the list - flying melon data up main growth ranking list (BiliBili platform) was released!
機器學習10-信念貝葉斯分類器
House change for agricultural products? "Disguised" house purchase subsidy!
【2022年】江西省研究生数学建模方案、代码
Machine learning 10 belief Bayesian classifier
CorelDRAW 2022中文精简64位直装版下载
AS400 entretien d'usine
URL和URI
How do the top ten securities firms open accounts? Also, is it safe to open an account online?