Longest Uncommon Subsequence | CPP program to find longest uncommon subsequence using naive method ; function to calculate length of longest uncommon subsequence ; creating an unordered map to map strings to their frequency ; traversing all elements of vector strArr ; Creating all possible subsequences , i . e 2 ^ n ; ( ( i >> j ) & 1 ) determines which character goes into string t ; If common subsequence is found , increment its frequency ; traversing the map ; if frequency equals 1 ; Driver code ; Input strings
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; int findLUSlength ( string a , string b ) { unordered_map < string , int > map ; vector < string > strArr ; strArr . push_back ( a ) ; strArr . push_back ( b ) ; for ( string s : strArr ) { for ( int i = 0 ; i < ( 1 << s . length ( ) ) ; i ++ ) { string t = " " ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { if ( ( ( i >> j ) & 1 ) != 0 ) t += s [ j ] ; } if ( map . count ( t ) ) map [ t ] ++ ; else map [ t ] = 1 ; } } int res = 0 ; for ( auto a : map ) { if ( a . second == 1 ) res = max ( res , ( int ) a . first . length ( ) ) ; } return res ; } int main ( ) { string a = " abcdabcd " , b = " abcabc " ; cout << findLUSlength ( a , b ) ; return 0 ; }