当前位置:网站首页>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 ).
s
Of Subsequence You can delete the strings
Implementation 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 : 3
Example 2:
Input : strs = ["aaa","aaa","aa"] Output : -1
Tips :
2 <= strs.length <= 50
1 <= strs[i].length <= 10
strs[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 }
边栏推荐
- 数学知识:求组合数 III—求组合数
- SWT / anr issues - ams/wms
- 7-2 punch in reward DP for puzzle a
- What is PMP?
- org.redisson.client.RedisResponseTimeoutException: Redis server response timeout (3000 ms)错误解决
- PMP是什么?
- P6773 [noi2020] destiny (DP, segment tree merging)
- The personal test is effective, and the JMeter desktop shortcut is quickly created
- Mathematical knowledge: finding combinatorial number III - finding combinatorial number
- Selenium经典面试题-多窗口切换解决方案
猜你喜欢
Qu'est - ce que le PMP?
Analysis on user behavior loss of data exploration e-commerce platform
[fundamentals of wireless communication-15]: illustrated mobile communication technology and application development-3-overview of digital communication 2G GSM, CDMA, 3G wdcma/cdma200/td-scdma, 4G LTE
int和位数组互转
FL studio20.9 fruit software advanced Chinese edition electronic music arrangement
计算特殊奖金
Mathematical knowledge: finding combinatorial number III - finding combinatorial number
electron之坑addon
The latest CSDN salary increase technology stack in 2022 overview of APP automated testing
The personal test is effective, and the JMeter desktop shortcut is quickly created
随机推荐
【JS】【掘金】获取关注了里不在关注者里的人
工厂+策略模式
7-2 punch in reward DP for puzzle a
Winodws 快速添加开机启动项
SWT / anr problem - SWT caused by long execution time of native method
QT web 开发 - video -- 笔记
Alphabet rearrange inator 3000 (dictionary tree custom sorting)
Calculate special bonus
Pytoch -- foundation refers to the north_ II. What high school students can understand [back propagation and gradient descent]
[fundamentals of wireless communication-15]: illustrated mobile communication technology and application development-3-overview of digital communication 2G GSM, CDMA, 3G wdcma/cdma200/td-scdma, 4G LTE
P6773 [NOI2020] 命运(dp、线段树合并)
The personal test is effective, and the JMeter desktop shortcut is quickly created
QML控件类型:ToolTip
Mathematical knowledge: 01 sequence satisfying conditions - find combinatorial number
Leetcode 面试题 17.10. 主要元素
[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大
AS400 large factory interview
Open source basic software companies, looking for you to create the future together (api7.ai)
Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)
Short message sending solution in medical his industry