Longest Uncommon Subsequence I in C++



Suppose we have two strings; we have to find the longest uncommon subsequence of these two strings. The longest uncommon subsequence is actually the longest subsequence of one string and this subsequence should not come in the other string. So, we have to find the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.

So, if the input is like "aabbac", "aabbcc", then the output will be 6

To solve this, we will follow these steps −

  • if a is same as b, then −

    • return -1

  • Otherwise

    • return maximum of size of a and size of b

Example 

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

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   int findLUSlength(string a, string b) {
      if (a == b)
         return -1;
      else
         return max(a.size(), b.size());
   }
};
main(){
   Solution ob;
   cout << (ob.findLUSlength("aabbac","aabbcc"));
}

Input

"aabbac","aabbcc"

Output

6
Updated on: 2020-06-10T13:04:06+05:30

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements