-
Notifications
You must be signed in to change notification settings - Fork 0
/
522.hpp
53 lines (45 loc) · 1.15 KB
/
522.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef LEETCODE_522_HPP
#define LEETCODE_522_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
bool isSubsequence(const string &s, const string &t) {
int si = 0;
for (char c : t) {
if (si < s.size() && s[si] == c)
si++;
}
return si == s.size();
}
class Solution {
public:
int findLUSlength(vector<string> &strs) {
sort(strs.begin(), strs.end(), [](const string &lhs, const string &rhs) -> bool {
return lhs.size() > rhs.size();
});
for (int i = 0; i < strs.size(); ++i) {
auto &str = strs[i];
bool all = true;
for (int j = 0; j < strs.size(); ++j) {
if (i == j)
continue;
if (isSubsequence(str, strs[j])) {
all = false;
break;
}
}
if (all)
return static_cast<int>(str.size());
}
return -1;
}
};
#endif //LEETCODE_522_HPP