forked from Ashish-kumar7/geeks-for-geeks-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_subsequence.cpp
More file actions
45 lines (42 loc) · 1.13 KB
/
Copy pathis_subsequence.cpp
File metadata and controls
45 lines (42 loc) · 1.13 KB
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
class Solution {
public:
int findLUSlength(vector<string>& strs)
{
sort(strs.begin(), strs.end(), [](auto& a, auto& b){return a.size() < b.size();});
unordered_map<string, int>mp;
string temp = "";
for (int i = strs.size() - 1; i >= 0; i--)
{
if (mp.find(strs[i]) != mp.end())
{
temp = strs[i];
mp[temp]++;
}
else if (!is_subsequence(temp, strs[i]))
{
mp[strs[i]]++;
}
}
int res = -1;
for (auto& x : mp)
{
if (x.second == 1)
res = max(res, int(x.first.size()));
}
return res;
}
bool is_subsequence(string a, string b)
{
int index_a = 0, index_b = 0;
while (index_a < a.size() && index_b < b.size())
{
while(index_a < a.size() && a[index_a] != b[index_b])
{
index_a++;
}
index_a++;
index_b++;
}
return index_b == b.size() && index_a <= a.size();
}
};