Longest Uncommon Subsequence | Java 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
import java . io . * ; import java . util . * ; class GfG { static int findLUSlength ( String a , String b ) { HashMap < String , Integer > map = new HashMap < String , Integer > ( ) ; Vector < String > strArr = new Vector < String > ( ) ; strArr . add ( a ) ; strArr . add ( 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 . charAt ( j ) ; } if ( map . containsKey ( t ) ) map . put ( t , map . get ( t ) + 1 ) ; else map . put ( t , 1 ) ; } } int res = 0 ; for ( HashMap . Entry < String , Integer > entry : map . entrySet ( ) ) { if ( entry . getValue ( ) == 1 ) res = Math . max ( res , entry . getKey ( ) . length ( ) ) ; } return res ; } public static void main ( String [ ] args ) { String a = " abcdabcd " , b = " abcabc " ; System . out . println ( findLUSlength ( a , b ) ) ; } }