给定字符串列表 strs ,返回其中 最长的特殊序列 。如果最长特殊序列不存在,返回 -1 。
特殊序列 定义如下:该序列为某字符串 独有的子序列(即不能是其他字符串的子序列)。
s 的 子序列可以通过删去字符串 s 中的某些字符实现。
例如,"abc" 是 "aebdc" 的子序列,因为您可以删除"aebdc"中的下划线字符来得到 "abc" 。"aebdc"的子序列还包括"aebdc"、 "aeb" 和 "" (空字符串)。
示例 1:
输入: strs = ["aba","cdc","eae"]
输出: 3
示例 2:
输入: strs = ["aaa","aaa","aa"]
输出: -1
class Solution {
public int findLUSlength(String[] strs) {
int res = -1;
for (int i = 0; i < strs.length; i++) {
boolean isMatch = false;
for (int j = 0; j < strs.length; j++) {
if (j != i) {
if (isSub(strs[i], strs[j])) {
isMatch = true;
}
}
}
if (!isMatch) {
res = Math.max(strs[i].length(), res);
}
}
return res;
}
public boolean isSub(String s1, String s2) {
int left = 0;
int right = 0;
while (true) {
if (left > s1.length()-1 || right > s2.length()-1 ) {
break;
}
if (s1.charAt(left) == s2.charAt(right)) {
left++;
right++;
} else {
right++;
}
}
if (left > s1.length()-1) {
return true; // 是子串
}
return false; // 不是子串
}
}