Shortest Uncommon Subsequence | A simple recursive C # 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
using System ; class GFG { static readonly 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 ; char [ ] St1 = new Char [ S . Length - 1 ] ; Array . Copy ( S , 1 , St1 , 0 , S . Length - 1 ) ; char [ ] St2 = new Char [ S . Length - 1 ] ; Array . Copy ( S , 1 , St2 , 0 , S . Length - 1 ) ; char [ ] Tt1 = new Char [ T . Length - ( k + 1 ) ] ; Array . Copy ( T , k + 1 , Tt1 , 0 , T . Length - ( k + 1 ) ) ; return Math . Min ( shortestSeq ( St1 , T , m - 1 , n ) , 1 + shortestSeq ( St2 , Tt1 , 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 ; Console . Write ( " Length ▁ of ▁ shortest ▁ subsequence ▁ is : ▁ " + ans + " STRNEWLINE " ) ; } }