
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Advertisements