codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum path in a matrix from top to bottom | C # implementation to find the maximum sum path in a matrix ; function to find the maximum sum path in a matrix ; if there is a single element only ; dp [ ] [ ] matrix to store the results of each iteration ; base case , copying elements of last row ; building up the dp [ ] [ ] matrix from bottom to the top row ; finding the maximum diagonal element in the ( i + 1 ) th row if that cell exists ; adding that ' max ' element to the mat [ i ] [ j ] element ; finding the maximum value from the first row of dp [ ] [ ] ; required maximum sum ; Driver code
using System ; class MaxSumPath { static int maxSum ( int [ , ] mat , int n ) { if ( n == 1 ) return mat [ 0 , 0 ] ; int [ , ] dp = new int [ n , n ] ; int maxSum = int . MinValue , max ; for ( int j = 0 ; j < n ; j ++ ) dp [ n - 1 , j ] = mat [ n - 1 , j ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { max = int . MinValue ; if ( ( ( j - 1 ) >= 0 ) && ( max < dp [ i + 1 , j - 1 ] ) ) max = dp [ i + 1 , j - 1 ] ; if ( ( ( j + 1 ) < n ) && ( max < dp [ i + 1 , j + 1 ] ) ) max = dp [ i + 1 , j + 1 ] ; dp [ i , j ] = mat [ i , j ] + max ; } } for ( int j = 0 ; j < n ; j ++ ) if ( maxSum < dp [ 0 , j ] ) maxSum = dp [ 0 , j ] ; return maxSum ; } public static void Main ( ) { int [ , ] mat = { { 5 , 6 , 1 , 7 } , { - 2 , 10 , 8 , - 1 } , { 3 , - 7 , - 9 , 11 } , { 12 , - 4 , 2 , 6 } } ; int n = 4 ; Console . WriteLine ( " Maximum ▁ Sum ▁ = ▁ " + maxSum ( mat , n ) ) ; } }
Longest Repeated Subsequence | C # program to find the longest repeated subsequence ; This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; THIS PART OF CODE IS SAME AS BELOW POST . IT FILLS dp [ , ] https : www . geeksforgeeks . org / longest - repeating - subsequence / OR the code mentioned above . ; THIS PART OF CODE FINDS THE RESULT STRING USING DP [ , ] Initialize result ; Traverse dp [ , ] from bottom right ; If this cell is same as diagonally adjacent cell just above it , then same characters are present at str [ i - 1 ] and str [ j - 1 ] . Append any of them to result . ; Otherwise we move to the side that that gave us maximum result ; Since we traverse dp [ , ] from bottom , we get result in reverse order . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static String longestRepeatedSubSeq ( String str ) { int n = str . Length , i , j ; int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) for ( j = 0 ; j <= n ; j ++ ) dp [ i , j ] = 0 ; for ( i = 1 ; i <= n ; i ++ ) for ( j = 1 ; j <= n ; j ++ ) if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = Math . Max ( dp [ i , j - 1 ] , dp [ i - 1 , j ] ) ; String res = " " ; i = n ; j = n ; while ( i > 0 && j > 0 ) { if ( dp [ i , j ] == dp [ i - 1 , j - 1 ] + 1 ) { res = res + str [ i - 1 ] ; i -- ; j -- ; } else if ( dp [ i , j ] == dp [ i - 1 , j ] ) i -- ; else j -- ; } String reverse = " " ; for ( int k = res . Length - 1 ; k >= 0 ; k -- ) { reverse = reverse + res [ k ] ; } return reverse ; } public static void Main ( String [ ] args ) { String str = " AABEBCDD " ; Console . WriteLine ( longestRepeatedSubSeq ( str ) ) ; } }
Sub | C # code to find the sub - tree with minimum color difference in a 2 - coloured tree ; Tree traversal to compute minimum difference ; Initial min difference is the color of node ; Traversing its children ; Not traversing the parent ; If the child is Adding positively to difference , we include it in the answer Otherwise , we leave the sub - tree and include 0 ( nothing ) in the answer ; DFS for colour difference : 1 colour - 2 colour ; Minimum colour difference is maximum answer value ; Clearing the current value to check for colour2 as well ; Interchanging the colours ; DFS for colour difference : 2 colour - 1 colour ; Checking if colour2 makes the minimum colour difference ; Driver code ; Nodes ; Adjacency list representation ; Edges ; Index represent the colour of that node There is no Node 0 , so we start from index 1 to N ; Printing the result
using System ; using System . Collections . Generic ; class GFG { static void dfs ( int node , int parent , List < int > [ ] tree , int [ ] colour , int [ ] answer ) { answer [ node ] = colour [ node ] ; foreach ( int u in tree [ node ] ) { if ( u == parent ) continue ; dfs ( u , node , tree , colour , answer ) ; answer [ node ] += Math . Max ( answer [ u ] , 0 ) ; } } static int maxDiff ( List < int > [ ] tree , int [ ] colour , int N ) { int [ ] answer = new int [ N + 1 ] ; dfs ( 1 , 0 , tree , colour , answer ) ; int high = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { high = Math . Max ( high , answer [ i ] ) ; answer [ i ] = 0 ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( colour [ i ] == - 1 ) colour [ i ] = 1 ; else colour [ i ] = - 1 ; } dfs ( 1 , 0 , tree , colour , answer ) ; for ( int i = 1 ; i < N ; i ++ ) high = Math . Max ( high , answer [ i ] ) ; return high ; } public static void Main ( String [ ] args ) { int N = 5 ; List < int > [ ] tree = new List < int > [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) tree [ i ] = new List < int > ( ) ; tree [ 1 ] . Add ( 2 ) ; tree [ 2 ] . Add ( 1 ) ; tree [ 1 ] . Add ( 3 ) ; tree [ 3 ] . Add ( 1 ) ; tree [ 2 ] . Add ( 4 ) ; tree [ 4 ] . Add ( 2 ) ; tree [ 3 ] . Add ( 5 ) ; tree [ 5 ] . Add ( 3 ) ; int [ ] colour = { 0 , 1 , 1 , - 1 , - 1 , 1 } ; Console . WriteLine ( maxDiff ( tree , colour , N ) ) ; } }
Remove array end element to maximize the sum of product | C # program to find maximum score we can get by removing elements from either end . ; If only one element left . ; If already calculated , return the value . ; Computing Maximum value when element at index i and index j is to be choosed . ; Driven code
using System ; class GFG { static int MAX = 50 ; static int solve ( int [ , ] dp , int [ ] a , int low , int high , int turn ) { if ( low == high ) return a [ low ] * turn ; if ( dp [ low , high ] != 0 ) return dp [ low , high ] ; dp [ low , high ] = Math . Max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a , low , high - 1 , turn + 1 ) ) ; return dp [ low , high ] ; } static void Main ( ) { int [ ] arr = new int [ ] { 1 , 3 , 1 , 5 , 2 } ; int n = arr . Length ; int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = 0 ; Console . Write ( solve ( dp , arr , 0 , n - 1 , 1 ) ) ; } }
Maximum sum bitonic subarray | C # implementation to find the maximum sum bitonic subarray ; function to find the maximum sum bitonic subarray ; ' msis [ ] ' to store the maximum sum increasing subarray up to each index of ' arr ' from the beginning ' msds [ ] ' to store the maximum sum decreasing subarray from each index of ' arr ' up to the end ; to store the maximum sum bitonic subarray ; building up the maximum sum increasing subarray for each array index ; building up the maximum sum decreasing subarray for each array index ; for each array index , calculating the maximum sum of bitonic subarray of which it is a part of ; if true , then update ' max ' bitonic subarray sum ; required maximum sum ; Driver Code
using System ; class GFG { static int maxSumBitonicSubArr ( int [ ] arr , int n ) { int [ ] msis = new int [ n ] ; int [ ] msds = new int [ n ] ; int max_sum = int . MinValue ; msis [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) msis [ i ] = msis [ i - 1 ] + arr [ i ] ; else msis [ i ] = arr [ i ] ; msds [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) if ( arr [ i ] > arr [ i + 1 ] ) msds [ i ] = msds [ i + 1 ] + arr [ i ] ; else msds [ i ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) if ( max_sum < ( msis [ i ] + msds [ i ] - arr [ i ] ) ) max_sum = msis [ i ] + msds [ i ] - arr [ i ] ; return max_sum ; } public static void Main ( ) { int [ ] arr = { 5 , 3 , 9 , 2 , 7 , 6 , 4 } ; int n = arr . Length ; Console . Write ( " Maximum ▁ Sum ▁ = ▁ " + maxSumBitonicSubArr ( arr , n ) ) ; } }
Find k | C # implementation to solve k queries for given n ranges ; Structure to store the start and end point ; Comparison function for sorting ; Function to find Kth smallest number in a Vector of merged intervals ; Traverse merged . get ( ) o find Kth smallest element using Linear search . ; To combined both type of ranges , overlapping as well as non - overlapping . ; Sorting intervals according to start time ; Merging all intervals into merged ; To check if starting point of next range is lying between the previous range and ending point of next range is greater than the Ending point of previous range then update ending point of previous range by ending point of next range . ; If starting point of next range is greater than the ending point of previous range then store next range in merged . get ( . ) if ( curr . s > prev . e ) ; Driver code ; Merge all intervals into merged . get ( ) ) ; Processing all queries on merged intervals
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public class Interval { public int s ; public int e ; public Interval ( int a , int b ) { s = a ; e = b ; } } ; class sortHelper : IComparer { int IComparer . Compare ( object a , object b ) { Interval first = ( Interval ) a ; Interval second = ( Interval ) b ; return first . s - second . s ; } } static int kthSmallestNum ( ArrayList merged , int k ) { int n = merged . Count ; for ( int j = 0 ; j < n ; j ++ ) { if ( k <= Math . Abs ( ( ( Interval ) merged [ j ] ) . e - ( ( Interval ) merged [ j ] ) . s + 1 ) ) return ( ( ( Interval ) merged [ j ] ) . s + k - 1 ) ; k = k - Math . Abs ( ( ( Interval ) merged [ j ] ) . e - ( ( Interval ) merged [ j ] ) . s + 1 ) ; } if ( k != 0 ) return - 1 ; return 0 ; } static ArrayList mergeIntervals ( ArrayList merged , Interval [ ] arr , int n ) { Array . Sort ( arr , new sortHelper ( ) ) ; merged . Add ( ( Interval ) arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { Interval prev = ( Interval ) merged [ merged . Count - 1 ] ; Interval curr = arr [ i ] ; if ( ( curr . s >= prev . s && curr . s <= prev . e ) && ( curr . e > prev . e ) ) { ( ( Interval ) merged [ merged . Count - 1 ] ) . e = ( ( Interval ) curr ) . e ; } else { merged . Add ( curr ) ; } } return merged ; } public static void Main ( string [ ] args ) { Interval [ ] arr = { new Interval ( 2 , 6 ) , new Interval ( 4 , 7 ) } ; int n = arr . Length ; int [ ] query = { 5 , 8 } ; int q = query . Length ; ArrayList merged = new ArrayList ( ) ; merged = mergeIntervals ( merged , arr , n ) ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( kthSmallestNum ( merged , query [ i ] ) ) ; } }
Shortest possible combination of two strings | C # program to print supersequence of two strings ; Prints super sequence of a [ 0. . m - 1 ] and b [ 0. . n - 1 ] ; Fill table in bottom up manner ; Below steps follow above recurrence ; Create a string of size index + 1 to store the result ; Start from the right - most - bottom - most corner and one by one store characters in res [ ] ; If current character in a [ ] and b are same , then current character is part of LCS ; Put current character in result ; reduce values of i , j and indexs ; If not same , then find the larger of two and go in the direction of larger value ; Copy remaining characters of string ' a ' ; Copy remaining characters of string ' b ' ; Print the result ; Driver program to test above function
using System ; public class GFG_1 { static void printSuperSeq ( string a , string b ) { int m = a . Length , n = b . Length ; int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i , j ] = j ; else if ( j == 0 ) dp [ i , j ] = i ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = 1 + Math . Min ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } string res = " " ; int k = m , l = n ; while ( k > 0 && l > 0 ) { if ( a [ k - 1 ] == b [ l - 1 ] ) { res = a [ k - 1 ] + res ; k -- ; l -- ; } else if ( dp [ k - 1 , l ] < dp [ k , l - 1 ] ) { res = a [ k - 1 ] + res ; k -- ; } else { res = b [ l - 1 ] + res ; l -- ; } } while ( k > 0 ) { res = a [ k - 1 ] + res ; k -- ; } while ( l > 0 ) { res = b [ l - 1 ] + res ; l -- ; } Console . WriteLine ( res ) ; } public static void Main ( ) { string a = " algorithm " ; string b = " rhythm " ; printSuperSeq ( a , b ) ; } }
Find length of longest subsequence of one string which is substring of another string | Base Case ; if the last char of both Strings are equal ; if the last char of both Strings are not equal ; Driver code ; as minimum length can be 0 only . ; traversing for every length of Y .
using System ; public class GFG { static int maxSubsequenceSubString ( String X , String Y , int n , int m ) { if ( n == 0 m == 0 ) return 0 ; if ( X [ n - 1 ] == Y [ m - 1 ] ) { return 1 + maxSubsequenceSubString ( X , Y , n - 1 , m - 1 ) ; } else { return maxSubsequenceSubString ( X , Y , n - 1 , m ) ; } } public static void Main ( String [ ] args ) { String X = " abcd " ; String Y = " bacdbdcd " ; int n = X . Length , m = Y . Length ; int maximum_length = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { int temp_ans = maxSubsequenceSubString ( X , Y , n , i ) ; if ( temp_ans > maximum_length ) maximum_length = temp_ans ; } Console . Write ( " Length ▁ for ▁ maximum ▁ possible ▁ Subsequence ▁ of ▁ String ▁ X ▁ which ▁ is ▁ SubString ▁ of ▁ Y - > " + maximum_length ) ; } }
Minimum time to write characters using insert , delete and copy operation | C # program to write characters in minimum time by inserting , removing and copying operation ; method returns minimum time to write ' N ' characters ; declare dp array and initialize with zero ; first char will always take insertion time ; loop for ' N ' number of times ; if current char count is even then choose minimum from result for ( i - 1 ) chars and time for insertion and result for half of chars and time for copy ; if current char count is odd then choose minimum from result for ( i - 1 ) chars and time for insertion and result for half of chars and time for copy and one extra character deletion ; Driver code
using System ; class GFG { static int minTimeForWritingChars ( int N , int insert , int remove , int copy ) { if ( N == 0 ) return 0 ; if ( N == 1 ) return insert ; int [ ] dp = new int [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = 0 ; dp [ 1 ] = insert ; for ( int i = 2 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) dp [ i ] = Math . Min ( dp [ i - 1 ] + insert , dp [ i / 2 ] + copy ) ; else dp [ i ] = Math . Min ( dp [ i - 1 ] + insert , dp [ ( i + 1 ) / 2 ] + copy + remove ) ; } return dp [ N ] ; } static void Main ( ) { int N = 9 ; int insert = 1 , remove = 2 , copy = 1 ; Console . Write ( minTimeForWritingChars ( N , insert , remove , copy ) ) ; } }
Count the number of ways to tile the floor of size n x m using 1 x m size tiles | C # implementation to count number of ways to tile a floor of size n x m using 1 x m tiles ; function to count the total number of ways ; table to store values of subproblems ; Fill the table upto value n ; recurrence relation ; base cases and i = m = 1 ; i = = m ; required number of ways ; Driver program
using System ; class GFG { static int countWays ( int n , int m ) { int [ ] count = new int [ n + 1 ] ; count [ 0 ] = 0 ; int i ; for ( i = 1 ; i <= n ; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ] ; else if ( i < m == 1 ) count [ i ] = 1 ; else count [ i ] = 2 ; } return count [ n ] ; } public static void Main ( ) { int n = 7 ; int m = 4 ; Console . Write ( " Number ▁ of ▁ ways ▁ = ▁ " + countWays ( n , m ) ) ; } }
Longest alternating subsequence | C # program for above approach ; Function for finding longest alternating subsequence ; " inc " and " dec " initialized as 1 , as single element is still LAS ; Iterate from second element ; " inc " changes iff " dec " changes ; " dec " changes iff " inc " changes ; Return the maximum length ; Driver code ; Function Call
using System ; class GFG { static int LAS ( int [ ] arr , int n ) { int inc = 1 ; int dec = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { inc = dec + 1 ; } else if ( arr [ i ] < arr [ i - 1 ] ) { dec = inc + 1 ; } } return Math . Max ( inc , dec ) ; } static void Main ( ) { int [ ] arr = { 10 , 22 , 9 , 33 , 49 , 50 , 31 , 60 } ; int n = arr . Length ; Console . WriteLine ( LAS ( arr , n ) ) ; } }
Minimum number of deletions to make a string palindrome | C # program for above approach ; Function to return minimum Element between two values ; Utility function for calculating Minimum element to delete ; Condition to compare characters ; Recursive function call ; Return value , increamenting by 1 ; Function to calculate the minimum Element required to delete for Making string pelindrom ; Utility function call ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int min ( int x , int y ) { return ( x < y ) ? x : y ; } static int utility_fun_for_del ( string str , int i , int j ) { if ( i >= j ) return 0 ; if ( str [ i ] == str [ j ] ) { return utility_fun_for_del ( str , i + 1 , j - 1 ) ; } return 1 + Math . Min ( utility_fun_for_del ( str , i + 1 , j ) , utility_fun_for_del ( str , i , j - 1 ) ) ; } static int min_ele_del ( string str ) { return utility_fun_for_del ( str , 0 , str . Length - 1 ) ; } static void Main ( ) { string str = " abefbac " ; Console . WriteLine ( " Minimum ▁ element ▁ of ▁ " + " deletions ▁ = ▁ " + min_ele_del ( str ) ) ; } }
Minimum number of deletions to make a string palindrome | Function definition ; Base cases ; Checking the ndesired condition ; If yes increment the cunt ; If no ; Return the value form the table ; Else store the max tranforamtion from the subsequence ; Return the dp [ - 1 ] [ - 1 ] ; Driver code ; Initialize the array with - 1
using System ; class GFG { static int [ , ] dp = new int [ 2000 , 2000 ] ; static int transformation ( string s1 , string s2 , int i , int j ) { if ( i >= ( s1 . Length ) || j >= ( s2 . Length ) ) { return 0 ; } if ( s1 [ i ] == s2 [ j ] ) { dp [ i , j ] = 1 + transformation ( s1 , s2 , i + 1 , j + 1 ) ; } if ( dp [ i , j ] != - 1 ) { return dp [ i , j ] ; } else { dp [ i , j ] = Math . Max ( transformation ( s1 , s2 , i , j + i ) , transformation ( s1 , s2 , i + 1 , j ) ) ; } return dp [ s1 . Length - 1 , s2 . Length - 1 ] ; } static public void Main ( ) { string s1 = " geeksforgeeks " ; string s2 = " geeks " ; for ( int m = 0 ; m < 2000 ; m ++ ) { for ( int n = 0 ; n < 2000 ; n ++ ) { dp [ m , n ] = - 1 ; } } Console . WriteLine ( " MINIMUM ▁ NUMBER ▁ OF ▁ DELETIONS : ▁ " + ( s1 . Length - transformation ( s1 , s2 , 0 , 0 ) ) ) ; Console . WriteLine ( " MINIMUM ▁ NUMBER ▁ OF ▁ INSERTIONS : ▁ " + ( s2 . Length - transformation ( s1 , s2 , 0 , 0 ) ) ) ; Console . WriteLine ( " LCS ▁ LENGTH : ▁ " + transformation ( s1 , s2 , 0 , 0 ) ) ; } }
Largest sum Zigzag sequence in a matrix | C # program to find the largest sum zigzag sequence ; Returns largest sum of a Zigzag sequence starting from ( i , j ) and ending at a bottom cell . ; If we have reached bottom ; Find the largest sum by considering all possible next elements in sequence . ; Returns largest possible sum of a Zizag sequence starting from top and ending at bottom . ; Consider all cells of top row as starting point ; Driver Code
using System ; class GFG { static int largestZigZagSumRec ( int [ , ] mat , int i , int j , int n ) { if ( i == n - 1 ) return mat [ i , j ] ; int zzs = 0 ; for ( int k = 0 ; k < n ; k ++ ) if ( k != j ) zzs = Math . Max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ; return zzs + mat [ i , j ] ; } static int largestZigZag ( int [ , ] mat , int n ) { int res = 0 ; for ( int j = 0 ; j < n ; j ++ ) res = Math . Max ( res , largestZigZagSumRec ( mat , 0 , j , n ) ) ; return res ; } public static void Main ( ) { int n = 3 ; int [ , ] mat = { { 4 , 2 , 1 } , { 3 , 9 , 6 } , { 11 , 3 , 15 } } ; Console . WriteLine ( " Largest ▁ zigzag ▁ sum : ▁ " + largestZigZag ( mat , n ) ) ; } }
Count ways to increase LCS length of two strings by one | C # program to get number of ways to increase LCS by 1 ; Method returns total ways to increase LCS length by 1 ; Fill positions of each character in vector ; Initializing 2D array by 0 values ; Filling LCS array for prefix substrings ; Filling LCS array for suffix substrings ; Looping for all possible insertion positions in first string ; Trying all possible lower case characters ; Now for each character , loop over same character positions in second string ; If both , left and right substrings make total LCS then increase result by 1 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int M = 26 ; static int waysToIncreaseLCSBy1 ( String str1 , String str2 ) { int m = str1 . Length , n = str2 . Length ; List < int > [ ] position = new List < int > [ M ] ; for ( int i = 0 ; i < M ; i ++ ) position [ i ] = new List < int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) position [ str2 [ i - 1 ] - ' a ' ] . Add ( i ) ; int [ , ] lcsl = new int [ m + 2 , n + 2 ] ; int [ , ] lcsr = new int [ m + 2 , n + 2 ] ; for ( int i = 0 ; i <= m + 1 ; i ++ ) for ( int j = 0 ; j <= n + 1 ; j ++ ) lcsl [ i , j ] = lcsr [ i , j ] = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) lcsl [ i , j ] = 1 + lcsl [ i - 1 , j - 1 ] ; else lcsl [ i , j ] = Math . Max ( lcsl [ i - 1 , j ] , lcsl [ i , j - 1 ] ) ; } } for ( int i = m ; i >= 1 ; i -- ) { for ( int j = n ; j >= 1 ; j -- ) { if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) lcsr [ i , j ] = 1 + lcsr [ i + 1 , j + 1 ] ; else lcsr [ i , j ] = Math . Max ( lcsr [ i + 1 , j ] , lcsr [ i , j + 1 ] ) ; } } int ways = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int d = 0 ; d < 26 ; d ++ ) { for ( int j = 0 ; j < position [ d ] . Count ; j ++ ) { int p = position [ d ] [ j ] ; if ( lcsl [ i , p - 1 ] + lcsr [ i + 1 , p + 1 ] == lcsl [ m , n ] ) ways ++ ; } } } return ways ; } public static void Main ( String [ ] args ) { String str1 = " abcabc " ; String str2 = " abcd " ; Console . WriteLine ( waysToIncreaseLCSBy1 ( str1 , str2 ) ) ; } }
Count of strings that can be formed using a , b and c under given constraints | C # program to count number of strings of n characters with a , b and c under given constraints ; n is total number of characters . bCount and cCount are counts of ' b ' and ' c ' respectively . ; Base cases ; Three cases , we choose , a or b or c . In all three cases n decreases by 1. ; Driver code ; Total number of characters
using System ; class GFG { static int countStr ( int n , int bCount , int cCount ) { if ( bCount < 0 cCount < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( bCount == 0 && cCount == 0 ) return 1 ; int res = countStr ( n - 1 , bCount , cCount ) ; res += countStr ( n - 1 , bCount - 1 , cCount ) ; res += countStr ( n - 1 , bCount , cCount - 1 ) ; return res ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( countStr ( n , 1 , 2 ) ) ; } }
Maximum path sum that starting with any cell of 0 | C # program to find Maximum path sum start any column in row '0' and ends up to any column in row ' n - 1' ; function find maximum sum path ; create 2D matrix to store the sum of the path ; copy all element of first column into ' dp ' first column ; Find maximum path sum that end ups at any column of last row ' N - 1' ; return maximum sum path ; driver code
using System ; class GFG { static int N = 4 ; static int MaximumPath ( int [ , ] Mat ) { int result = 0 ; int [ , ] dp = new int [ N , N + 2 ] ; for ( int i = 0 ; i < N ; i ++ ) dp [ 0 , i + 1 ] = Mat [ 0 , i ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j <= N ; j ++ ) dp [ i , j ] = Math . Max ( dp [ i - 1 , j - 1 ] , Math . Max ( dp [ i - 1 , j ] , dp [ i - 1 , j + 1 ] ) ) + Mat [ i , j - 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) result = Math . Max ( result , dp [ N - 1 , i ] ) ; return result ; } public static void Main ( ) { int [ , ] Mat = { { 4 , 2 , 3 , 4 } , { 2 , 9 , 1 , 10 } , { 15 , 1 , 3 , 0 } , { 16 , 92 , 41 , 44 } } ; Console . WriteLine ( MaximumPath ( Mat ) ) ; } }