- Related Questions & Answers
- Longest Common Subsequence in C++
- Longest Palindromic Subsequence in C++
- Longest Harmonious Subsequence in C++
- Longest Common Subsequence
- Longest Bitonic Subsequence
- Longest Increasing Subsequence
- Longest Palindromic Subsequence
- Longest Continuous Increasing Subsequence in C++
- C++ Program for Longest Common Subsequence
- Number of Longest Increasing Subsequence in C++
- Length of Longest Fibonacci Subsequence in C++
- Longest Increasing Subsequence in Python
- Longest Arithmetic Subsequence of Given Difference in C++
- Java Program for Longest Increasing Subsequence
- Java Program for Longest Common Subsequence

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

#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")); }

"aabbac","aabbcc"

6

Advertisements