Shortest Uncommon Subsequence | A simple recursive Java program to find shortest uncommon subsequence . ; A recursive function to find the length of shortest uncommon subsequence ; S String is empty ; T String is empty ; Loop to search for current character ; char not found in T ; Return minimum of following two Not including current char in answer Including current char ; Driver code
import java . util . * ; class GFG { static final int MAX = 1005 ; static int shortestSeq ( char [ ] S , char [ ] T , int m , int n ) { if ( m == 0 ) return MAX ; if ( n <= 0 ) return 1 ; int k ; for ( k = 0 ; k < n ; k ++ ) if ( T [ k ] == S [ 0 ] ) break ; if ( k == n ) return 1 ; return Math . min ( shortestSeq ( Arrays . copyOfRange ( S , 1 , S . length ) , T , m - 1 , n ) , 1 + shortestSeq ( Arrays . copyOfRange ( S , 1 , S . length ) , Arrays . copyOfRange ( T , k + 1 , T . length ) , m - 1 , n - k - 1 ) ) ; } public static void main ( String [ ] args ) { char S [ ] = " babab " . toCharArray ( ) ; char T [ ] = " babba " . toCharArray ( ) ; int m = S . length , n = T . length ; int ans = shortestSeq ( S , T , m , n ) ; if ( ans >= MAX ) ans = - 1 ; System . out . print ( " Length ▁ of ▁ shortest ▁ subsequence ▁ is : ▁ " + ans + "NEW_LINE"); } }