-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLongest_Uncommon_Subsequence_II.cpp
39 lines (37 loc) · 1.1 KB
/
Longest_Uncommon_Subsequence_II.cpp
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
class Solution {
bool isSubseq(string const& s1, string const& s2) {
if(s1.length() > s2.length()) {
return false;
}
int j = 0;
for(int i = 0; i < (int)s2.length() and j < (int)s1.length(); i++) {
if(s2[i] == s1[j]) {
j++;
}
}
return j == (int)s1.length();
}
public:
int findLUSlength(vector<string>& strs) {
int result = -1;
unordered_set<string> exist;
for(int i = 0; i < (int)strs.size(); i++) {
// silly prune
if(exist.find(strs[i]) != exist.end() or (int)strs[i].length() <= result) {
continue;
}
bool flag = true;
for(int j = 0; j < (int)strs.size() and flag; j++) {
if(i == j) continue;
if(isSubseq(strs[i], strs[j])) {
flag = false;
}
}
exist.insert(strs[i]);
if(flag) {
result = max(result, (int)strs[i].length());
}
}
return result;
}
};