給定字符串列表 strs 碌奉,返回其中 最長的特殊序列 。如果最長特殊序列不存在,返回 -1 。
特殊序列 定義如下:該序列為某字符串 獨有的子序列(即不能是其他字符串的子序列)自沧。
s 的 子序列可以通過刪去字符串 s 中的某些字符實現(xiàn)。
例如树瞭,"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; // 不是子串
}
}