
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 −
#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
- Related Articles
- Longest Uncommon Subsequence II in C++
- Longest Common Subsequence
- Longest Bitonic Subsequence
- Longest Increasing Subsequence
- Longest Palindromic Subsequence
- Finding the longest "uncommon" sequence in JavaScript
- Longest Increasing Subsequence in Python
- Longest Common Subsequence in C++
- Longest Harmonious Subsequence in C++
- Longest Palindromic Subsequence in C++
- Finding the longest substring uncommon in array in JavaScript
- Longest Continuous Increasing Subsequence in C++
- Longest decreasing subsequence subarray in JavaScript
- Number of Longest Increasing Subsequence in C++
- Length of Longest Fibonacci Subsequence in C++

Advertisements