-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path522-longest-uncommon-subsequence-ii.cpp
85 lines (76 loc) · 1.94 KB
/
522-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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Solution {
public:
vector<string> _str;
int findLUSlength(vector<string>& strs) {
int len = strs.size();
if (len == 0)
{
return -1;
}
if (len == 1)
{
return strs[0].size();
}
sort(strs.begin(), strs.end(), [](string a, string b) -> bool {if (a.size() != b.size()) return a.size() > b.size(); int i = 0; while (i < a.size()){if (a[i] != b[i]) return a[i] < b[i]; i++;} return false;});
int i = 0;
int max_length = strs[0].size();
_str.push_back(strs[0]);
while (strs[i].size() == max_length)
{
if (strs[i] != strs[i + 1])
{
return max_length;
}
while (strs[i] == strs[i + 1])
{
i++;
}
i++;
}
for (int j = i; j < len; j++)
{
if (j != len - 1)
{
if (strs[j] == strs[j + 1])
{
while (j != len - 1 && strs[j] == strs[j + 1])
{
j++;
}
_str.push_back(strs[j]);
continue;
}
}
if (find(strs[j]))
{
return strs[j].size();
}
}
return -1;
}
bool find(string ss)
{
for (auto s : _str)
{
int i = 0;
int j = 0;
while (i < s.size() && j < ss.size())
{
if (s[i] == ss[j])
{
i++;
j++;
}
else
{
i++;
}
}
if (j == ss.size())
{
return false;
}
}
return true;
}
};