给定字符串列表,你需要从它们中找出最长的特殊序列。最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列)。
子序列可以通过删去字符串中的某些字符实现,但不能改变剩余字符的相对顺序。空序列为所有字符串的子序列,任何字符串为其自身的子序列。
输入将是一个字符串列表,输出是最长特殊序列的长度。如果最长特殊序列不存在,返回 -1 。
示例:
输入: "aba", "cdc", "eae"
输出: 3
提示:
- 所有给定的字符串长度不会超过 10 。
- 给定字符串列表的长度将在 [2, 50 ] 之间。
class Solution {
public:
int findLUSlength(vector<string>& strs) {
int res = -1, j = 0, n = strs.size();
for (int i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (i == j) continue;
if (checkSubs(strs[i], strs[j])) break;
}
if (j == n) res = max(res, (int)strs[i].size());
}
return res;
}
int checkSubs(string subs, string str) {
int i = 0;
for (char c : str) {
if (c == subs[i]) ++i;
if (i == subs.size()) break;
}
return i == subs.size();
}
};