
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Longest Uncommon Subsequence II in C++
- Longest Common Subsequence in C++
- Longest Palindromic Subsequence in C++
- Longest Harmonious Subsequence in C++
- Longest Continuous Increasing Subsequence in C++
- Longest Common Subsequence
- Longest Bitonic Subsequence
- Longest Increasing Subsequence
- Longest Palindromic Subsequence
- 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++
- Longest decreasing subsequence subarray in JavaScript
Advertisements