当前位置:网站首页>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 }
边栏推荐
- What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss
- Leetcode(524)——通过删除字母匹配到字典里最长单词
- How to learn and read code
- (总结一)Halcon基础之寻找目标特征+转正
- Windows quick add boot entry
- Int and bit group turn to each other
- The personal test is effective, and the JMeter desktop shortcut is quickly created
- Selenium classic interview question - multi window switching solution
- 数学知识:求组合数 III—求组合数
- My PMP learning test experience
猜你喜欢

go导入自建包

Leetcode 面试题 17.10. 主要元素

Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。

Electron pit Addon

(翻译)使用眉状文本提高标题点击率

electron之坑addon

What are the applications of SMS in enterprises?

In the fourth week of June, the list - flying melon data up main growth ranking list (BiliBili platform) was released!

聚焦绿色低碳,数据中心散热进入“智能冷却”新时代

Selenium经典面试题-多窗口切换解决方案
随机推荐
The whole process of AS400 API from zero to one
PMP是什么?
现在开户有优惠吗?另外,手机开户安全么?
What is project management?
Pytorch —— 基础指北_贰 高中生都能看懂的[反向传播和梯度下降]
数学知识:满足条件的01序列—求组合数
数学知识:求组合数 IV—求组合数
org.redisson.client.RedisResponseTimeoutException: Redis server response timeout (3000 ms)错误解决
对象与对象变量
What is the difference between port number and process number?
Log4j2 ThreadContext日志链路追踪
(翻译)实时内联验证更容易让用户犯错的原因
Machine learning 10 belief Bayesian classifier
数学知识:求组合数 III—求组合数
SWT/ANR问题--StorageManagerService卡住
SWT / anr problem - binder stuck
【agora】用户管理
十大劵商如何开户?还有,在线开户安全么?
工厂+策略模式
In the fourth week of June, the list - flying melon data up main growth ranking list (BiliBili platform) was released!