Longest Uncommon Subsequence | C # 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
using System ; using System . Collections . Generic ; class GFG { static int findLUSlength ( string a , string b ) { Dictionary < string , int > map = new Dictionary < string , int > ( ) ; List < string > strArr = new List < string > ( ) ; strArr . Add ( a ) ; strArr . Add ( b ) ; foreach ( string s in 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 . ContainsKey ( t ) ) { int value = map [ t ] + 1 ; map . Remove ( t ) ; map . Add ( t , value ) ; } else map . Add ( t , 1 ) ; } } int res = 0 ; foreach ( KeyValuePair < string , int > entry in map ) { if ( entry . Value == 1 ) res = Math . Max ( res , entry . Key . Length ) ; } return res ; } static void Main ( ) { string a = " abcdabcd " , b = " abcabc " ; Console . Write ( findLUSlength ( a , b ) ) ; } }