# Longest Uncommon Subsequence II in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Suppose we have a list of strings; we have to find the longest uncommon subsequence among them. The longest uncommon subsequence is actually the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

We know that a subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements.

Here we will take a list of strings, and the output needs to be the length of the longest uncommon subsequence. If there is no longest uncommon subsequence, then return -1.

So, if the input is like "aba", "cdc", "eae", then the output will be 3

To solve this, we will follow these steps −

• Define a function isSubsequence(), this will take a, b,

• j := 0

• for initialize i := 0, when i < size of a, update (increase i by 1), do −

• if j < size of b and a[i] is same as b[j], then −

• (increase j by 1)

• return true when size of b is same as j

• Define a function getDuplicates(), this will take an array strs,

• Define one set visited and another set ret

• for initialize i := 0, when i < size of strs, update (increase i by 1), do −

• if strs[i] is in visited, then −

• insert strs[i] into ret

• insert strs[i] into visited

• return ret

• From the main method, do the following −

• sort the array strs based on the strings length

• Define one set duplicates := getDuplicates(strs)

• for initialize i := 0, when i < size of strs, update (increase i by 1), do −

• if strs[i] is in duplicates, then −

• if i is same as 0, then −

• return size of strs[i]

• for initialize j := 0, when j < i, update (increase j by 1), do −

• if isSubsequence(strs[j], strs[i]) is false, then −

• if j is same as i - 1, then −

• return size of strs[i]

• Otherwise

• Come out from the loop

• return -1

## Example

Let us see the following implementation to get a better understanding −

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
static bool cmp(string a, string b){
return a.size() > b.size();
}
int findLUSlength(vector<string>& strs){
sort(strs.begin(), strs.end(), cmp);
set<string> duplicates = getDuplicates(strs);
for (int i = 0; i < strs.size(); i++) {
if (duplicates.count(strs[i]))
continue;
if (i == 0)
return strs[i].size();
for (int j = 0; j < i; j++) {
if (!isSubsequence(strs[j], strs[i])) {
if ((j == i - 1)) {
cout << i << endl;
return strs[i].size();
}
}
else
break;
}
}
return -1;
}
bool isSubsequence(string a, string b){
int j = 0;
for (int i = 0; i < a.size(); i++) {
if (j < b.size() && a[i] == b[j])
j++;
}
return j == b.size();
}
set<string> getDuplicates(vector<string>& strs){
set<string> visited;
set<string> ret;
for (int i = 0; i < strs.size(); i++) {
if (visited.count(strs[i])) {
ret.insert(strs[i]);
}
visited.insert(strs[i]);
}
return ret;
}
};
main(){
Solution ob;
vector<string> v = {"aba", "cdc", "eae"};
cout << (ob.findLUSlength(v));
}

## Input

{"aba", "cdc", "eae"}

## Output

3
Updated on 17-Nov-2020 11:26:18