codeparrot/xlcost-text-to-code huggingface dataset:
Longest Common Subsequence | DP | C # Naive recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Utility function to get max of 2 integers ; Driver code
using System ; class GFG { static int lcs ( char [ ] X , char [ ] Y , int m , int n ) { if ( m == 0 n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } static int max ( int a , int b ) { return ( a > b ) ? a : b ; } public static void Main ( ) { String s1 = " AGGTAB " ; String s2 = " GXTXAYB " ; char [ ] X = s1 . ToCharArray ( ) ; char [ ] Y = s2 . ToCharArray ( ) ; int m = X . Length ; int n = Y . Length ; Console . Write ( " Length ▁ of ▁ LCS ▁ is " + " ▁ " + lcs ( X , Y , m , n ) ) ; } }
Longest Common Subsequence | DP | Dynamic Programming C # implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] ; Utility function to get max of 2 integers ; Driver code
using System ; class GFG { static int lcs ( char [ ] X , char [ ] Y , int m , int n ) { int [ , ] L = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i , j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; else L [ i , j ] = max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } return L [ m , n ] ; } static int max ( int a , int b ) { return ( a > b ) ? a : b ; } public static void Main ( ) { String s1 = " AGGTAB " ; String s2 = " GXTXAYB " ; char [ ] X = s1 . ToCharArray ( ) ; char [ ] Y = s2 . ToCharArray ( ) ; int m = X . Length ; int n = Y . Length ; Console . Write ( " Length ▁ of ▁ LCS ▁ is " + " ▁ " + lcs ( X , Y , m , n ) ) ; } }
Edit Distance | DP |  ; If any string is empty , return the remaining characters of other string ; To check if the recursive tree for given n & m has already been executed ; If characters are equal , execute recursive function for n - 1 , m - 1 ; If characters are nt equal , we need to find the minimum cost out of all 3 operations . ; temp variables ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minDis ( string s1 , string s2 , int n , int m , List < List < int > > dp ) { if ( n == 0 ) return m ; if ( m == 0 ) return n ; if ( dp [ n ] [ m ] != - 1 ) return dp [ n ] [ m ] ; if ( s1 [ n - 1 ] == s2 [ m - 1 ] ) { if ( dp [ n - 1 ] [ m - 1 ] == - 1 ) { return dp [ n ] [ m ] = minDis ( s1 , s2 , n - 1 , m - 1 , dp ) ; } else return dp [ n ] [ m ] = dp [ n - 1 ] [ m - 1 ] ; } else { int m1 , m2 , m3 ; if ( dp [ n - 1 ] [ m ] != - 1 ) { m1 = dp [ n - 1 ] [ m ] ; } else { m1 = minDis ( s1 , s2 , n - 1 , m , dp ) ; } if ( dp [ n ] [ m - 1 ] != - 1 ) { m2 = dp [ n ] [ m - 1 ] ; } else { m2 = minDis ( s1 , s2 , n , m - 1 , dp ) ; } if ( dp [ n - 1 ] [ m - 1 ] != - 1 ) { m3 = dp [ n - 1 ] [ m - 1 ] ; } else { m3 = minDis ( s1 , s2 , n - 1 , m - 1 , dp ) ; } return dp [ n ] [ m ] = 1 + Math . Min ( m1 , Math . Min ( m2 , m3 ) ) ; } } static void Main ( ) { string str1 = " voldemort " ; string str2 = " dumbledore " ; int n = str1 . Length , m = str2 . Length ; List < List < int > > dp = new List < List < int > > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { dp . Add ( new List < int > ( ) ) ; for ( int j = 0 ; j < m + 1 ; j ++ ) { dp [ i ] . Add ( - 1 ) ; } } Console . WriteLine ( minDis ( str1 , str2 , n , m , dp ) ) ; } }
Min Cost Path | DP | A Naive recursive implementation of MCP ( Minimum Cost Path ) problem ; A utility function that returns minimum of 3 integers ; Returns cost of minimum cost path from ( 0 , 0 ) to ( m , n ) in mat [ R ] [ C ] ; Driver code
using System ; class GFG { static int min ( int x , int y , int z ) { if ( x < y ) return ( ( x < z ) ? x : z ) ; else return ( ( y < z ) ? y : z ) ; } static int minCost ( int [ , ] cost , int m , int n ) { if ( n < 0 m < 0 ) return int . MaxValue ; else if ( m == 0 && n == 0 ) return cost [ m , n ] ; else return cost [ m , n ] + min ( minCost ( cost , m - 1 , n - 1 ) , minCost ( cost , m - 1 , n ) , minCost ( cost , m , n - 1 ) ) ; } public static void Main ( ) { int [ , ] cost = { { 1 , 2 , 3 } , { 4 , 8 , 2 } , { 1 , 5 , 3 } } ; Console . Write ( minCost ( cost , 2 , 2 ) ) ; } }
Min Cost Path | DP | C # program for Dynamic Programming implementation of Min Cost Path problem ; Instead of following line , we can use int tc [ m + 1 ] [ n + 1 ] or dynamically allocate memory to save space . The following line is used to keep the program simple and make it working on all compilers . ; Initialize first column of total cost ( tc ) array ; Initialize first row of tc array ; Construct rest of the tc array ; A utility function that returns minimum of 3 integers ; Driver program
using System ; class GFG { private static int minCost ( int [ , ] cost , int m , int n ) { int i , j ; int [ , ] tc = new int [ m + 1 , n + 1 ] ; tc [ 0 , 0 ] = cost [ 0 , 0 ] ; for ( i = 1 ; i <= m ; i ++ ) tc [ i , 0 ] = tc [ i - 1 , 0 ] + cost [ i , 0 ] ; for ( j = 1 ; j <= n ; j ++ ) tc [ 0 , j ] = tc [ 0 , j - 1 ] + cost [ 0 , j ] ; for ( i = 1 ; i <= m ; i ++ ) for ( j = 1 ; j <= n ; j ++ ) tc [ i , j ] = min ( tc [ i - 1 , j - 1 ] , tc [ i - 1 , j ] , tc [ i , j - 1 ] ) + cost [ i , j ] ; return tc [ m , n ] ; } private static int min ( int x , int y , int z ) { if ( x < y ) return ( x < z ) ? x : z ; else return ( y < z ) ? y : z ; } public static void Main ( ) { int [ , ] cost = { { 1 , 2 , 3 } , { 4 , 8 , 2 } , { 1 , 5 , 3 } } ; Console . Write ( minCost ( cost , 2 , 2 ) ) ; } }
0 | A Dynamic Programming based solution for 0 - 1 Knapsack problem ; A utility function that returns maximum of two integers ; Returns the maximum value that can be put in a knapsack of capacity W ; Build table K [ ] [ ] in bottom up manner ; Driver code
using System ; class GFG { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int knapSack ( int W , int [ ] wt , int [ ] val , int n ) { int i , w ; int [ , ] K = new int [ n + 1 , W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 w == 0 ) K [ i , w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i , w ] = Math . Max ( val [ i - 1 ] + K [ i - 1 , w - wt [ i - 1 ] ] , K [ i - 1 , w ] ) ; else K [ i , w ] = K [ i - 1 , w ] ; } } return K [ n , W ] ; } static void Main ( ) { int [ ] val = new int [ ] { 60 , 100 , 120 } ; int [ ] wt = new int [ ] { 10 , 20 , 30 } ; int W = 50 ; int n = val . Length ; Console . WriteLine ( knapSack ( W , wt , val , n ) ) ; } }
Longest Palindromic Subsequence | DP | A Dynamic Programming based C # Program for the Egg Dropping Puzzle ; A utility function to get max of two integers ; Returns the length of the longest palindromic subsequence in seq ; Create a table to store results of subproblems ; Strings of length 1 are palindrome of lentgh 1 ; Build the table . Note that the lower diagonal values of table are useless and not filled in the process . The values are filled in a manner similar to Matrix Chain Multiplication DP solution ( See www . geeksforgeeks . org / matrix - chain - multiplication - dp - 8 / https : cl is length of substring ; Driver program to test above functions
using System ; class GFG { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static int lps ( string seq ) { int n = seq . Length ; int i , j , cl ; int [ , ] L = new int [ n , n ] ; for ( i = 0 ; i < n ; i ++ ) L [ i , i ] = 1 ; for ( cl = 2 ; cl <= n ; cl ++ ) { for ( i = 0 ; i < n - cl + 1 ; i ++ ) { j = i + cl - 1 ; if ( seq [ i ] == seq [ j ] && cl == 2 ) L [ i , j ] = 2 ; else if ( seq [ i ] == seq [ j ] ) L [ i , j ] = L [ i + 1 , j - 1 ] + 2 ; else L [ i , j ] = max ( L [ i , j - 1 ] , L [ i + 1 , j ] ) ; } } return L [ 0 , n - 1 ] ; } public static void Main ( ) { string seq = " GEEKS ▁ FOR ▁ GEEKS " ; int n = seq . Length ; Console . Write ( " The ▁ length ▁ of ▁ the ▁ " + " lps ▁ is ▁ " + lps ( seq ) ) ; } }
Palindrome Partitioning | DP |  ; minCut function ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minCut ( string a ) { int [ ] cut = new int [ a . Length ] ; bool [ , ] palindrome = new bool [ a . Length , a . Length ] ; for ( int i = 0 ; i < a . Length ; i ++ ) { int minCut = i ; for ( int j = 0 ; j <= i ; j ++ ) { if ( a [ i ] == a [ j ] && ( i - j < 2 palindrome [ j + 1 , i - 1 ] ) ) { palindrome [ j , i ] = true ; minCut = Math . Min ( minCut , j == 0 ? 0 : ( cut [ j - 1 ] + 1 ) ) ; } } cut [ i ] = minCut ; } return cut [ a . Length - 1 ] ; } static void Main ( ) { Console . WriteLine ( minCut ( " aab " ) ) ; Console . WriteLine ( minCut ( " aabababaxx " ) ) ; } }
Word Wrap Problem | DP | A Dynamic programming solution for Word Wrap Problem in Java ; A utility function to print the solution ; l [ ] represents lengths of different words in input sequence . For example , l [ ] = { 3 , 2 , 2 , 5 } is for a sentence like " aaa ▁ bb ▁ cc ▁ ddddd " . n is size of l [ ] and M is line width ( maximum no . of characters that can fit in a line ) ; For simplicity , 1 extra space is used in all below arrays extras [ i ] [ j ] will have number of extra spaces if words from i to j are put in a single line ; lc [ i ] [ j ] will have cost of a line which has words from i to j ; c [ i ] will have total cost of optimal arrangement of words from 1 to i ; p [ ] is used to print the solution . ; calculate extra spaces in a single line . The value extra [ i ] [ j ] indicates extra spaces if words from word number i to j are placed in a single line ; Calculate line cost corresponding to the above calculated extra spaces . The value lc [ i ] [ j ] indicates cost of putting words from word number i to j in a single line ; Calculate minimum cost and find minimum cost arrangement . The value c [ j ] indicates optimized cost to arrange words from word number 1 to j . ; Driver code
using System ; public class GFG { static int MAX = int . MaxValue ; static int printSolution ( int [ ] p , int n ) { int k ; if ( p [ n ] == 1 ) k = 1 ; else k = printSolution ( p , p [ n ] - 1 ) + 1 ; Console . WriteLine ( " Line ▁ number " + " ▁ " + k + " : ▁ From ▁ word ▁ no . " + " ▁ " + p [ n ] + " ▁ " + " to " + " ▁ " + n ) ; return k ; } static void solveWordWrap ( int [ ] l , int n , int M ) { int [ , ] extras = new int [ n + 1 , n + 1 ] ; int [ , ] lc = new int [ n + 1 , n + 1 ] ; int [ ] c = new int [ n + 1 ] ; int [ ] p = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { extras [ i , i ] = M - l [ i - 1 ] ; for ( int j = i + 1 ; j <= n ; j ++ ) extras [ i , j ] = extras [ i , j - 1 ] - l [ j - 1 ] - 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j ++ ) { if ( extras [ i , j ] < 0 ) lc [ i , j ] = MAX ; else if ( j == n && extras [ i , j ] >= 0 ) lc [ i , j ] = 0 ; else lc [ i , j ] = extras [ i , j ] * extras [ i , j ] ; } } c [ 0 ] = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { c [ j ] = MAX ; for ( int i = 1 ; i <= j ; i ++ ) { if ( c [ i - 1 ] != MAX && lc [ i , j ] != MAX && ( c [ i - 1 ] + lc [ i , j ] < c [ j ] ) ) { c [ j ] = c [ i - 1 ] + lc [ i , j ] ; p [ j ] = i ; } } } printSolution ( p , n ) ; } public static void Main ( ) { int [ ] l = { 3 , 2 , 2 , 5 } ; int n = l . Length ; int M = 6 ; solveWordWrap ( l , n , M ) ; } }
Ugly Numbers | C # program to find nth ugly number ; This function divides a by greatest divisible power of b ; Function to check if a number is ugly or not ; Function to get the nth ugly number ; ugly number count ; Check for all integers until count becomes n ; Driver code
using System ; class GFG { static int maxDivide ( int a , int b ) { while ( a % b == 0 ) a = a / b ; return a ; } static int isUgly ( int no ) { no = maxDivide ( no , 2 ) ; no = maxDivide ( no , 3 ) ; no = maxDivide ( no , 5 ) ; return ( no == 1 ) ? 1 : 0 ; } static int getNthUglyNo ( int n ) { int i = 1 ; int count = 1 ; while ( n > count ) { i ++ ; if ( isUgly ( i ) == 1 ) count ++ ; } return i ; } public static void Main ( ) { int no = getNthUglyNo ( 150 ) ; Console . WriteLine ( "150th ▁ ugly " + " ▁ no . ▁ is ▁ " + no ) ; } }
Longest Palindromic Substring | Set 1 | A C # solution for longest palindrome ; Function to print a subString str [ low . . high ] ; This function prints the longest palindrome subString It also returns the length of the longest palindrome ; get length of input String ; All subStrings of length 1 are palindromes ; Nested loop to mark start and end index ; Check palindrome ; Palindrome ; return length of LPS ; Driver Code
using System ; class GFG { static void printSubStr ( String str , int low , int high ) { for ( int i = low ; i <= high ; ++ i ) Console . Write ( str [ i ] ) ; } static int longestPalSubstr ( String str ) { int n = str . Length ; int maxLength = 1 , start = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { for ( int j = i ; j < str . Length ; j ++ ) { int flag = 1 ; for ( int k = 0 ; k < ( j - i + 1 ) / 2 ; k ++ ) if ( str [ i + k ] != str [ j - k ] ) flag = 0 ; if ( flag != 0 && ( j - i + 1 ) > maxLength ) { start = i ; maxLength = j - i + 1 ; } } } Console . Write ( " longest ▁ palindrome ▁ subString ▁ is : ▁ " ) ; printSubStr ( str , start , start + maxLength - 1 ) ; return maxLength ; } public static void Main ( String [ ] args ) { String str = " forgeeksskeegfor " ; Console . Write ( " Length is : " + longestPalSubstr ( str ) ) ; } }
Longest Palindromic Substring | Set 1 | C # Solution ; A utility function to print a substring str [ low ... ( high - ( low + 1 ) ) ] ; This function prints the longest palindrome substring of str [ ] . It also returns the length of the longest palindrome ; Get length of input string ; Table [ i , j ] will be false if substring str [ i . . j ] is not palindrome . Else table [ i , j ] will be true ; All substrings of length 1 are palindromes ; Check for sub - string of length 2. ; Check for lengths greater than 2. k is length of substring ; Fix the starting index ; Get the ending index of substring from starting index i and length k ; Checking for sub - string from ith index to jth index iff str . charAt ( i + 1 ) to str . charAt ( j - 1 ) is a palindrome ; Return length of LPS ; Driver code
using System ; class GFG { static void printSubStr ( string str , int low , int high ) { Console . WriteLine ( str . Substring ( low , high - low + 1 ) ) ; } static int longestPalSubstr ( string str ) { int n = str . Length ; bool [ , ] table = new bool [ n , n ] ; int maxLength = 1 ; for ( int i = 0 ; i < n ; ++ i ) table [ i , i ] = true ; int start = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( str [ i ] == str [ i + 1 ] ) { table [ i , i + 1 ] = true ; start = i ; maxLength = 2 ; } } for ( int k = 3 ; k <= n ; ++ k ) { for ( int i = 0 ; i < n - k + 1 ; ++ i ) { int j = i + k - 1 ; if ( table [ i + 1 , j - 1 ] && str [ i ] == str [ j ] ) { table [ i , j ] = true ; if ( k > maxLength ) { start = i ; maxLength = k ; } } } } Console . Write ( " Longest ▁ palindrome ▁ substring ▁ is : ▁ " ) ; printSubStr ( str , start , start + maxLength - 1 ) ; return maxLength ; } public static void Main ( string [ ] args ) { string str = " forgeeksskeegfor " ; Console . WriteLine ( " Length ▁ is : ▁ " + longestPalSubstr ( str ) ) ; } }
Optimal Binary Search Tree | DP | A naive recursive implementation of optimal binary search tree problem ; A utility function to get sum of array elements freq [ i ] to freq [ j ] ; A recursive function to calculate cost of optimal binary search tree ; Base cases no elements in this subarray ; one element in this subarray ; Get sum of freq [ i ] , freq [ i + 1 ] , ... freq [ j ] ; Initialize minimum value ; One by one consider all elements as root and recursively find cost of the BST , compare the cost with min and update min if needed ; Return minimum value ; The main function that calculates minimum cost of a Binary Search Tree . It mainly uses optCost ( ) to find the optimal cost . ; Here array keys [ ] is assumed to be sorted in increasing order . If keys [ ] is not sorted , then add code to sort keys , and rearrange freq [ ] accordingly . ; Driver code
using System ; class GFG { static int sum ( int [ ] freq , int i , int j ) { int s = 0 ; for ( int k = i ; k <= j ; k ++ ) s += freq [ k ] ; return s ; } static int optCost ( int [ ] freq , int i , int j ) { if ( j < i ) return 0 ; if ( j == i ) return freq [ i ] ; int fsum = sum ( freq , i , j ) ; int min = int . MaxValue ; for ( int r = i ; r <= j ; ++ r ) { int cost = optCost ( freq , i , r - 1 ) + optCost ( freq , r + 1 , j ) ; if ( cost < min ) min = cost ; } return min + fsum ; } static int optimalSearchTree ( int [ ] keys , int [ ] freq , int n ) { return optCost ( freq , 0 , n - 1 ) ; } public static void Main ( ) { int [ ] keys = { 10 , 12 , 20 } ; int [ ] freq = { 34 , 8 , 50 } ; int n = keys . Length ; Console . Write ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is ▁ " + optimalSearchTree ( keys , freq , n ) ) ; } }
Optimal Binary Search Tree | DP | Dynamic Programming C # code for Optimal Binary Search Tree Problem ; A utility function to get sum of array elements freq [ i ] to freq [ j ] ; A Dynamic Programming based function that calculates minimum cost of a Binary Search Tree . ; Create an auxiliary 2D matrix to store results of subproblems ; For a single key , cost is equal to frequency of the key ; Now we need to consider chains of length 2 , 3 , ... . L is chain length . ; i is row number in cost [ ] [ ] ; Get column number j from row number i and chain length L ; Try making all keys in interval keys [ i . . j ] as root ; c = cost when keys [ r ] becomes root of this subtree ; Driver program to test above functions
using System ; class GFG { static int sum ( int [ ] freq , int i , int j ) { int s = 0 ; for ( int k = i ; k <= j ; k ++ ) { if ( k >= freq . Length ) continue ; s += freq [ k ] ; } return s ; } static int optimalSearchTree ( int [ ] keys , int [ ] freq , int n ) { int [ , ] cost = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) cost [ i , i ] = freq [ i ] ; for ( int L = 2 ; L <= n ; L ++ ) { for ( int i = 0 ; i <= n - L + 1 ; i ++ ) { int j = i + L - 1 ; cost [ i , j ] = int . MaxValue ; for ( int r = i ; r <= j ; r ++ ) { int c = ( ( r > i ) ? cost [ i , r - 1 ] : 0 ) + ( ( r < j ) ? cost [ r + 1 , j ] : 0 ) + sum ( freq , i , j ) ; if ( c < cost [ i , j ] ) cost [ i , j ] = c ; } } } return cost [ 0 , n - 1 ] ; } public static void Main ( ) { int [ ] keys = { 10 , 12 , 20 } ; int [ ] freq = { 34 , 8 , 50 } ; int n = keys . Length ; Console . Write ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is ▁ " + optimalSearchTree ( keys , freq , n ) ) ; } }
Largest Independent Set Problem | DP | C # program for calculating LISS using dynamic programming ; A binary tree node has data , pointer to left child and a pointer to right child ; A memoization function returns size of the largest independent set in a given binary tree ; Calculate size excluding the current node ; Calculate size including the current node ; Maximum of two sizes is LISS , store it for future uses . ; Driver code ; Let us construct the tree given in the above diagram
using System ; class LisTree { public class node { public int data , liss ; public node left , right ; public node ( int data ) { this . data = data ; this . liss = 0 ; } } static int liss ( node root ) { if ( root == null ) return 0 ; if ( root . liss != 0 ) return root . liss ; if ( root . left == null && root . right == null ) return root . liss = 1 ; int liss_excl = liss ( root . left ) + liss ( root . right ) ; int liss_incl = 1 ; if ( root . left != null ) { liss_incl += ( liss ( root . left . left ) + liss ( root . left . right ) ) ; } if ( root . right != null ) { liss_incl += ( liss ( root . right . left ) + liss ( root . right . right ) ) ; } return root . liss = Math . Max ( liss_excl , liss_incl ) ; } public static void Main ( String [ ] args ) { node root = new node ( 20 ) ; root . left = new node ( 8 ) ; root . left . left = new node ( 4 ) ; root . left . right = new node ( 12 ) ; root . left . right . left = new node ( 10 ) ; root . left . right . right = new node ( 14 ) ; root . right = new node ( 22 ) ; root . right . right = new node ( 25 ) ; Console . WriteLine ( " Size ▁ of ▁ the ▁ Largest ▁ Independent ▁ Set ▁ is ▁ " + liss ( root ) ) ; } }
Boolean Parenthesization Problem | DP | C # program of above approach ; Returns count of all possible parenthesizations that lead to result true for a boolean expression with symbols like true and false and operators like & , | and ^ filled between symbols ; Fill diaginal entries first All diagonal entries in T [ i , i ] are 1 if symbol [ i ] is T ( true ) . Similarly , all F [ i , i ] entries are 1 if symbol [ i ] is F ( False ) ; Now fill T [ i , i + 1 ] , T [ i , i + 2 ] , T [ i , i + 3 ] ... in order And F [ i , i + 1 ] , F [ i , i + 2 ] , F [ i , i + 3 ] ... in order ; Find place of parenthesization using current value of gap ; Store Total [ i , k ] and Total [ k + 1 , j ] ; Follow the recursive formulas according to the current operator ; Driver code ; There are 4 ways ( ( T T ) & ( F ^ T ) ) , ( T | ( T & ( F ^ T ) ) ) , ( ( ( T T ) & F ) ^ T ) and ( T | ( ( T & F ) ^ T ) )
using System ; class GFG { static int countParenth ( char [ ] symb , char [ ] oper , int n ) { int [ , ] F = new int [ n , n ] ; int [ , ] T = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { F [ i , i ] = ( symb [ i ] == ' F ' ) ? 1 : 0 ; T [ i , i ] = ( symb [ i ] == ' T ' ) ? 1 : 0 ; } for ( int gap = 1 ; gap < n ; ++ gap ) { for ( int i = 0 , j = gap ; j < n ; ++ i , ++ j ) { T [ i , j ] = F [ i , j ] = 0 ; for ( int g = 0 ; g < gap ; g ++ ) { int k = i + g ; int tik = T [ i , k ] + F [ i , k ] ; int tkj = T [ k + 1 , j ] + F [ k + 1 , j ] ; if ( oper [ k ] == ' & ' ) { T [ i , j ] += T [ i , k ] * T [ k + 1 , j ] ; F [ i , j ] += ( tik * tkj - T [ i , k ] * T [ k + 1 , j ] ) ; } if ( oper [ k ] == ' ▁ ' ) { F [ i , j ] += F [ i , k ] * F [ k + 1 , j ] ; T [ i , j ] += ( tik * tkj - F [ i , k ] * F [ k + 1 , j ] ) ; } if ( oper [ k ] == ' ^ ' ) { T [ i , j ] += F [ i , k ] * T [ k + 1 , j ] + T [ i , k ] * F [ k + 1 , j ] ; F [ i , j ] += T [ i , k ] * T [ k + 1 , j ] + F [ i , k ] * F [ k + 1 , j ] ; } } } } return T [ 0 , n - 1 ] ; } public static void Main ( ) { char [ ] symbols = " TTFT " . ToCharArray ( ) ; char [ ] operators = " | & ^ " . ToCharArray ( ) ; int n = symbols . Length ; Console . WriteLine ( countParenth ( symbols , operators , n ) ) ; } }