codeparrot/xlcost-text-to-code huggingface dataset:
Minimum insertions to form a palindrome | DP | A C # solution for Dynamic Programming based program to find minimum number insertions needed to make a string palindrome ; A DP function to find minimum number of insertions ; Create a table of size n * n . table [ i ] [ j ] will store minimum number of insertions needed to convert str [ i . . j ] to a palindrome . ; Fill the table ; Return minimum number of insertions for str [ 0. . n - 1 ] ; Driver code
using System ; class GFG { static int findMinInsertionsDP ( char [ ] str , int n ) { int [ , ] table = new int [ n , n ] ; int l , h , gap ; for ( gap = 1 ; gap < n ; ++ gap ) for ( l = 0 , h = gap ; h < n ; ++ l , ++ h ) table [ l , h ] = ( str [ l ] == str [ h ] ) ? table [ l + 1 , h - 1 ] : ( Math . Min ( table [ l , h - 1 ] , table [ l + 1 , h ] ) + 1 ) ; return table [ 0 , n - 1 ] ; } public static void Main ( ) { String str = " geeks " ; Console . Write ( findMinInsertionsDP ( str . ToCharArray ( ) , str . Length ) ) ; } }
Maximum Subarray Sum using Divide and Conquer algorithm | A Divide and Conquer based C # program for maximum subarray sum problem ; Find the maximum possible sum in arr [ ] such that arr [ m ] is part of it ; Include elements on left of mid . ; Include elements on right of mid ; Return sum of elements on left and right of mid returning only left_sum + right_sum will fail for [ - 2 , 1 ] ; Returns sum of maximum sum subarray in aa [ l . . h ] ; Base Case : Only one element ; Find middle point ; Return maximum of following three possible cases : a ) Maximum subarray sum in left half b ) Maximum subarray sum in right half c ) Maximum subarray sum such that the subarray crosses the midpoint ; Driver program to test maxSubArraySum
using System ; class GFG { static int maxCrossingSum ( int [ ] arr , int l , int m , int h ) { int sum = 0 ; int left_sum = int . MinValue ; for ( int i = m ; i >= l ; i -- ) { sum = sum + arr [ i ] ; if ( sum > left_sum ) left_sum = sum ; } sum = 0 ; int right_sum = int . MinValue ; ; for ( int i = m + 1 ; i <= h ; i ++ ) { sum = sum + arr [ i ] ; if ( sum > right_sum ) right_sum = sum ; } return Math . Max ( left_sum + right_sum , Math . Max ( left_sum , right_sum ) ) ; } static int maxSubArraySum ( int [ ] arr , int l , int h ) { if ( l == h ) return arr [ l ] ; int m = ( l + h ) / 2 ; return Math . Max ( Math . Max ( maxSubArraySum ( arr , l , m ) , maxSubArraySum ( arr , m + 1 , h ) ) , maxCrossingSum ( arr , l , m , h ) ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 7 } ; int n = arr . Length ; int max_sum = maxSubArraySum ( arr , 0 , n - 1 ) ; Console . Write ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + max_sum ) ; } }
Program to find amount of water in a given glass | Program to find the amount of water in j - th glass of i - th row ; Returns the amount of water in jth glass of ith row ; A row number i has maximum i columns . So input column number must be less than i ; There will be i * ( i + 1 ) / 2 glasses till ith row ( including ith row ) ; Put all water in first glass ; Now let the water flow to the downward glasses till the row number is less than or / equal to i ( given row ) correction : X can be zero for side glasses as they have lower rate to fill ; Fill glasses in a given row . Number of columns in a row is equal to row number ; Get the water from current glass ; Keep the amount less than or equal to capacity in current glass ; Get the remaining amount ; Distribute the remaining amount to the down two glasses ; The index of jth glass in ith row will be i * ( i - 1 ) / 2 + j - 1 ; Driver Code ; Total amount of water
using System ; class GFG { static float findWater ( int i , int j , float X ) { if ( j > i ) { Console . WriteLine ( " Incorrect ▁ Input " ) ; Environment . Exit ( 0 ) ; } int ll = ( int ) Math . Round ( ( double ) ( i * ( i + 1 ) ) ) ; float [ ] glass = new float [ ll + 2 ] ; int index = 0 ; glass [ index ] = X ; for ( int row = 1 ; row <= i ; ++ row ) { for ( int col = 1 ; col <= row ; ++ col , ++ index ) { X = glass [ index ] ; glass [ index ] = ( X >= 1.0f ) ? 1.0f : X ; X = ( X >= 1.0f ) ? ( X - 1 ) : 0.0f ; glass [ index + row ] += X / 2 ; glass [ index + row + 1 ] += X / 2 ; } } return glass [ ( int ) ( i * ( i - 1 ) / 2 + j - 1 ) ] ; } static void Main ( ) { int i = 2 , j = 2 ; float X = 2.0f ; Console . WriteLine ( " Amount ▁ of ▁ water ▁ in ▁ jth ▁ " + " glass ▁ of ▁ ith ▁ row ▁ is : ▁ " + findWater ( i , j , X ) ) ; } }
Maximum Length Chain of Pairs | DP | Dynamic C # program to find Maximum Length Chain of Pairs ; This function assumes that arr [ ] is sorted in increasing order according the first ( or smaller ) values in pairs . ; Initialize MCL ( max chain length ) values for all indexes ; Compute optimized chain length values in bottom up manner ; Pick maximum of all MCL values ; Driver Code
using System ; class Pair { int a ; int b ; public Pair ( int a , int b ) { this . a = a ; this . b = b ; } static int maxChainLength ( Pair [ ] arr , int n ) { int i , j , max = 0 ; int [ ] mcl = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) mcl [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] . a > arr [ j ] . b && mcl [ i ] < mcl [ j ] + 1 ) mcl [ i ] = mcl [ j ] + 1 ; for ( i = 0 ; i < n ; i ++ ) if ( max < mcl [ i ] ) max = mcl [ i ] ; return max ; } public static void Main ( ) { Pair [ ] arr = new Pair [ ] { new Pair ( 5 , 24 ) , new Pair ( 15 , 25 ) , new Pair ( 27 , 40 ) , new Pair ( 50 , 60 ) } ; Console . Write ( " Length ▁ of ▁ maximum ▁ size STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL chain ▁ is ▁ " + maxChainLength ( arr , arr . Length ) ) ; } }
Palindrome Partitioning | DP | C # Code for Palindrome Partitioning Problem ; Returns the minimum number of cuts needed to partition a string such that every part is a palindrome ; Get the length of the string ; Create two arrays to build the solution in bottom up manner C [ i ] [ j ] = Minimum number of cuts needed for palindrome partitioning of substring str [ i . . j ] P [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false Note that C [ i ] [ j ] is 0 if P [ i ] [ j ] is true ; different looping variables ; Every substring of length 1 is a palindrome ; L is substring length . Build the solution in bottom up manner by considering all substrings of length starting from 2 to n . The loop structure is same as Matrix Chain Multiplication problem ( See https : www . geeksforgeeks . org / matrix - chain - multiplication - dp - 8 / ) ; For substring of length L , set different possible starting indexes ; Set ending index ; If L is 2 , then we just need to compare two characters . Else need to check two corner characters and value of P [ i + 1 ] [ j - 1 ] ; IF str [ i . . j ] is palindrome , then C [ i ] [ j ] is 0 ; Make a cut at every possible localtion starting from i to j , and get the minimum cost cut . ; Return the min cut value for complete string . i . e . , str [ 0. . n - 1 ] ; Driver program
using System ; class GFG { static int minPalPartion ( String str ) { int n = str . Length ; int [ , ] C = new int [ n , n ] ; bool [ , ] P = new bool [ n , n ] ; int i , j , k , L ; for ( i = 0 ; i < n ; i ++ ) { P [ i , i ] = true ; C [ i , i ] = 0 ; } for ( L = 2 ; L <= n ; L ++ ) { for ( i = 0 ; i < n - L + 1 ; i ++ ) { j = i + L - 1 ; if ( L == 2 ) P [ i , j ] = ( str [ i ] == str [ j ] ) ; else P [ i , j ] = ( str [ i ] == str [ j ] ) && P [ i + 1 , j - 1 ] ; if ( P [ i , j ] == true ) C [ i , j ] = 0 ; else { C [ i , j ] = int . MaxValue ; for ( k = i ; k <= j - 1 ; k ++ ) C [ i , j ] = Math . Min ( C [ i , j ] , C [ i , k ] + C [ k + 1 , j ] + 1 ) ; } } } return C [ 0 , n - 1 ] ; } public static void Main ( ) { String str = " ababbbabbababa " ; Console . Write ( " Min ▁ cuts ▁ needed ▁ for ▁ " + " Palindrome ▁ Partitioning ▁ is ▁ " + minPalPartion ( str ) ) ; } }
Count subtrees that sum up to a given value x only using single recursive function | c # program to find if there is a subtree with given sum ; structure of a node of binary tree ; function to get a new node ; allocate space ; put in the data ; function to count subtress that sum up to a given value x ; if tree is empty ; sum of nodes in the left subtree ; sum of nodes in the right subtree ; sum of nodes in the subtree rooted with ' root . data ' ; if true ; return subtree 's nodes sum ; utility function to count subtress that sum up to a given value x ; if tree is empty ; sum of nodes in the left subtree ; sum of nodes in the right subtree ; if tree 's nodes sum == x ; required count of subtrees ; Driver Code ; binary tree creation 5 / \ - 10 3 / \ / \ 9 8 - 4 7
using System ; public class GFG { public class Node { public int data ; public Node left , right ; } public class INT { public int v ; public INT ( int a ) { v = a ; } } public static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } public static int countSubtreesWithSumX ( Node root , INT count , int x ) { if ( root == null ) { return 0 ; } int ls = countSubtreesWithSumX ( root . left , count , x ) ; int rs = countSubtreesWithSumX ( root . right , count , x ) ; int sum = ls + rs + root . data ; if ( sum == x ) { count . v ++ ; } return sum ; } public static int countSubtreesWithSumXUtil ( Node root , int x ) { if ( root == null ) { return 0 ; } INT count = new INT ( 0 ) ; int ls = countSubtreesWithSumX ( root . left , count , x ) ; int rs = countSubtreesWithSumX ( root . right , count , x ) ; if ( ( ls + rs + root . data ) == x ) { count . v ++ ; } return count . v ; } public static void Main ( string [ ] args ) { Node root = getNode ( 5 ) ; root . left = getNode ( - 10 ) ; root . right = getNode ( 3 ) ; root . left . left = getNode ( 9 ) ; root . left . right = getNode ( 8 ) ; root . right . left = getNode ( - 4 ) ; root . right . right = getNode ( 7 ) ; int x = 7 ; Console . WriteLine ( " Count ▁ = ▁ " + countSubtreesWithSumXUtil ( root , x ) ) ; } }
Minimum replacements required to make given Matrix palindromic | C # program for the above approach ; Function to count minimum changes to make the matrix palindromic ; Rows in the matrix ; Columns in the matrix ; Traverse the given matrix ; Store the frequency of the four cells ; Iterate over the map ; Min changes to do to make all ; Four elements equal ; Make the middle row palindromic ; Make the middle column palindromic ; Print minimum changes ; Driver Code ; Given matrix ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void minchanges ( int [ , ] mat ) { int N = mat . GetLength ( 0 ) ; int M = mat . GetLength ( 1 ) ; int i , j , ans = 0 , x ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( i = 0 ; i < N / 2 ; i ++ ) { for ( j = 0 ; j < M / 2 ; j ++ ) { if ( mp . ContainsKey ( mat [ i , M - 1 - j ] ) ) { mp [ mat [ i , M - 1 - j ] ] = mp [ mat [ i , M - 1 - j ] ] + 1 ; } else { mp . Add ( mat [ i , M - 1 - j ] , 1 ) ; } if ( mp . ContainsKey ( mat [ i , j ] ) ) { mp [ mat [ i , j ] ] = mp [ mat [ i , j ] ] + 1 ; } else { mp . Add ( mat [ i , j ] , 1 ) ; } if ( mp . ContainsKey ( mat [ N - 1 - i , M - 1 - j ] ) ) { mp [ mat [ N - 1 - i , M - 1 - j ] ] = mp [ mat [ N - 1 - i , M - 1 - j ] ] + 1 ; } else { mp . Add ( mat [ N - 1 - i , M - 1 - j ] , 1 ) ; } if ( mp . ContainsKey ( mat [ N - 1 - i , j ] ) ) { mp [ mat [ N - 1 - i , j ] ] = mp [ mat [ N - 1 - i , j ] ] + 1 ; } else { mp . Add ( mat [ N - 1 - i , j ] , 1 ) ; } x = 0 ; foreach ( KeyValuePair < int , int > it in mp ) { x = Math . Max ( x , it . Value ) ; } ans = ans + 4 - x ; mp . Clear ( ) ; } } if ( N % 2 == 1 ) { for ( i = 0 ; i < M / 2 ; i ++ ) { if ( mat [ N / 2 , i ] != mat [ N / 2 , M - 1 - i ] ) ans ++ ; } } if ( M % 2 == 1 ) { for ( i = 0 ; i < N / 2 ; i ++ ) { if ( mat [ i , M / 2 ] != mat [ N - 1 - i , M / 2 ] ) ans ++ ; } } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 3 } , { 1 , 2 , 1 } } ; minchanges ( mat ) ; } }
Check if a number starts with another number or not | C # program for the above approach ; Function to check if B is a prefix of A or not ; Convert numbers into Strings ; Check if s2 is a prefix of s1 or not using starts_with ( ) function ; If result is true , print " Yes " ; Driver code ; Given numbers ; Function call
using System . Threading ; using System . Globalization ; using System ; class GFG { static void checkprefix ( int A , int B ) { string s1 = A . ToString ( ) ; string s2 = B . ToString ( ) ; bool result ; result = s1 . StartsWith ( s2 , false , CultureInfo . InvariantCulture ) ; if ( result ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } static void Main ( ) { int A = 12345 , B = 12 ; checkprefix ( A , B ) ; } }
Count subtrees that sum up to a given value x only using single recursive function | C # program to find if there is a subtree with given sum ; Node class to create new node ; Utility function to count subtress that sum up to a given value x ; Driver Code ; binary tree creation 5 / \ - 10 3 / \ / \ 9 8 - 4 7
using System ; public class Node { public int data ; public Node left ; public Node right ; public Node ( int data ) { this . data = data ; } } class GFG { static int count = 0 ; static Node ptr ; int countSubtreesWithSumXUtil ( Node root , int x ) { int l = 0 , r = 0 ; if ( root == null ) return 0 ; l += countSubtreesWithSumXUtil ( root . left , x ) ; r += countSubtreesWithSumXUtil ( root . right , x ) ; if ( l + r + root . data == x ) count ++ ; if ( ptr != root ) return l + root . data + r ; return count ; } public static void Main ( string [ ] args ) { Node root = new Node ( 5 ) ; root . left = new Node ( - 10 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 9 ) ; root . left . right = new Node ( 8 ) ; root . right . left = new Node ( - 4 ) ; root . right . right = new Node ( 7 ) ; int x = 7 ; ptr = root ; Console . Write ( " Count ▁ = ▁ " + new GFG ( ) . countSubtreesWithSumXUtil ( root , x ) ) ; } }
Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity | C # implementation to find the minimum flips required such that alternate subarrays have different parity ; Function to find the minimum flips required in binary array ; Boolean value to indicate odd or even value of 1 's ; Loop to iterate over the subarrays of size K ; curr_index is used to iterate over all the subarrays ; Loop to iterate over the array at the jump of K to consider every parity ; Condition to check if the subarray is at even position ; The value needs to be same as the first subarray ; The value needs to be opposite of the first subarray ; Update the minimum difference ; Condition to check if the 1 s in the subarray is odd ; Driver Code
using System ; class Count_Flips { static int count_flips ( int [ ] a , int n , int k ) { bool set = false ; int ans = 0 , min_diff = int . MaxValue ; for ( int i = 0 ; i < k ; i ++ ) { int curr_index = i , segment = 0 , count_zero = 0 , count_one = 0 ; while ( curr_index < n ) { if ( segment % 2 == 0 ) { if ( a [ curr_index ] == 1 ) count_zero ++ ; else count_one ++ ; } else { if ( a [ curr_index ] == 0 ) count_zero ++ ; else count_one ++ ; } curr_index = curr_index + k ; segment ++ ; } ans += Math . Min ( count_one , count_zero ) ; if ( count_one < count_zero ) set = ! set ; min_diff = Math . Min ( min_diff , Math . Abs ( count_zero - count_one ) ) ; } if ( set ) return ans ; else return ans + min_diff ; } public static void Main ( string [ ] args ) { int n = 6 , k = 3 ; int [ ] a = { 0 , 0 , 1 , 1 , 0 , 0 } ; Console . WriteLine ( count_flips ( a , n , k ) ) ; } }
Program to build a DFA that checks if a string ends with "01" or "10" | C # Program to DFA that accepts string ending with 01 or 10. ; End position is checked using the string length value . q0 is the starting state . q1 and q2 are intermediate states . q3 and q4 are final states . ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q1 , 1 takes to q2 ; Driver Code ; all state transitions are printed . if string is accpetable , YES is printed . else NO is printed
using System ; class GFG { static void q1 ( string s , int i ) { Console . Write ( " q1 - > " ) ; if ( i == s . Length ) { Console . WriteLine ( " NO " ) ; return ; } if ( s [ i ] == '0' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q2 ( string s , int i ) { Console . Write ( " q2 - > " ) ; if ( i == s . Length ) { Console . WriteLine ( " NO ▁ " ) ; return ; } if ( s [ i ] == '0' ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q3 ( string s , int i ) { Console . Write ( " q3 - > " ) ; if ( i == s . Length ) { Console . WriteLine ( " YES " ) ; return ; } if ( s [ i ] == '0' ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q4 ( string s , int i ) { Console . Write ( " q4 - > " ) ; if ( i == s . Length ) { Console . WriteLine ( " YES " ) ; return ; } if ( s [ i ] == '0' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q0 ( string s , int i ) { Console . Write ( " q0 - > " ) ; if ( i == s . Length ) { Console . WriteLine ( " NO " ) ; return ; } if ( s [ i ] == '0' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } public static void Main ( ) { string s = "010101" ; Console . Write ( " State ▁ transitions ▁ are ▁ " ) ; q0 ( s , 0 ) ; } }
Check whether two strings contain same characters in same order | C # implementation of the approach ; if length of the b = 0 then we return true ; if length of a = 0 that means b is not present in a so we return false ; Driver code
using System ; public class temp { public static bool checkSequence ( String a , String b ) { if ( b . Length == 0 ) return true ; if ( a . Length == 0 ) return false ; if ( a [ 0 ] == b [ 0 ] ) return checkSequence ( a . Substring ( 1 ) , b . Substring ( 1 ) ) ; else return checkSequence ( a . Substring ( 1 ) , b ) ; } public static void Main ( String [ ] args ) { String s1 = " Geeks " , s2 = " Geks " ; if ( checkSequence ( s1 , s2 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
String matching with * ( that matches with any ) in any of the two strings | C # program for string matching with ; Function to check if the two strings can be matched or not ; if the string don 't have *  then character at that position  must be same. ; Driver code
using System ; class GfG { public static int doMatch ( String A , String B ) { for ( int i = 0 ; i < A . Length ; i ++ ) { if ( A [ i ] != ' * ' && B [ i ] != ' * ' ) if ( A [ i ] != B [ i ] ) return 0 ; } return 1 ; } public static void Main ( String [ ] args ) { String A = " gee * sforgeeks " ; String B = " geeksforgeeks " ; Console . WriteLine ( doMatch ( A , B ) ) ; } }
Find Nth term of the series 0 , 2 , 4 , 8 , 12 , 18. . . | C # program to find N - th term of the series : 0 , 2 , 4 , 8 , 12 , 18. . . ; Calculate Nth term of series ; Driver program
using System ; class gfg { public int nthTerm ( int N ) { int n = ( ( N + N * ( N - 1 ) ) / 2 ) ; return n ; } static void Main ( string [ ] args ) { gfg p = new gfg ( ) ; int a = p . nthTerm ( 5 ) ; Console . WriteLine ( a ) ; Console . Read ( ) ; } }
Number of substrings of one string present in other | C # program to count number of substrings of s1 present in s2 . ; s3 stores all substrings of s1 ; check the presence of s3 in s2 ; Driver code
using System ; class GFG { static int countSubstrs ( String s1 , String s2 ) { int ans = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { String s3 = " " ; char [ ] s4 = s1 . ToCharArray ( ) ; for ( int j = i ; j < s1 . Length ; j ++ ) { s3 += s4 [ j ] ; if ( s2 . IndexOf ( s3 ) != - 1 ) ans ++ ; } } return ans ; } public static void Main ( String [ ] args ) { String s1 = " aab " , s2 = " aaaab " ; Console . WriteLine ( countSubstrs ( s1 , s2 ) ) ; } }
Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | C # program to find N - th term of the series : 1 , 4 , 15 , 72 , 420 ; Function to find factorial of N ; return factorial of N ; calculate Nth term of series ; Driver Code
using System ; class GFG { static int factorial ( int N ) { int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) fact = fact * i ; return fact ; } static int nthTerm ( int N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } public static void Main ( ) { int N = 6 ; Console . Write ( nthTerm ( N ) ) ; } }
Sum of nodes at maximum depth of a Binary Tree | C # code to find the sum of the nodes which are present at the maximum depth . ; Function to return a new node ; Function to find the sum of the node which are present at the maximum depth . While traversing the nodes compare the level of the node with max_level ( Maximum level till the current node ) . If the current level exceeds the maximum level , update the max_level as current level . If the max level and current level are same , add the root data to current sum . ; Driver Code
using System ; class GFG { static int sum = 0 , max_level = int . MinValue ; public class Node { public int d ; public Node l ; public Node r ; } ; static Node createNode ( int d ) { Node node ; node = new Node ( ) ; node . d = d ; node . l = null ; node . r = null ; return node ; } static void sumOfNodesAtMaxDepth ( Node ro , int level ) { if ( ro == null ) return ; if ( level > max_level ) { sum = ro . d ; max_level = level ; } else if ( level == max_level ) { sum = sum + ro . d ; } sumOfNodesAtMaxDepth ( ro . l , level + 1 ) ; sumOfNodesAtMaxDepth ( ro . r , level + 1 ) ; } public static void Main ( String [ ] args ) { Node root ; root = createNode ( 1 ) ; root . l = createNode ( 2 ) ; root . r = createNode ( 3 ) ; root . l . l = createNode ( 4 ) ; root . l . r = createNode ( 5 ) ; root . r . l = createNode ( 6 ) ; root . r . r = createNode ( 7 ) ; sumOfNodesAtMaxDepth ( root , 0 ) ; Console . WriteLine ( sum ) ; } }
Match Expression where a single special character in pattern can match one or more characters | C # program for pattern matching where a single special character can match one more characters ; Returns true if pat matches with text . ; i is used as an index in pattern and j as an index in text . ; Traverse through pattern ; If current character of pattern is not ' # ' ; If does not match with text . ; If matches , increment i and j ; Current character is ' # ' ; At least one character must match with # ; Match characters with # until a matching character is found . ; Matching with # is over , move ahead in pattern ; Driver code
using System ; class GFG { public static bool regexMatch ( String text , String pat ) { int lenText = text . Length ; int lenPat = pat . Length ; char [ ] Text = text . ToCharArray ( ) ; char [ ] Pat = pat . ToCharArray ( ) ; int i = 0 , j = 0 ; while ( i < lenPat ) { if ( Pat [ i ] != ' # ' ) { if ( Pat [ i ] != Text [ j ] ) return false ; i ++ ; j ++ ; } else { j ++ ; while ( Text [ j ] != Pat [ i + 1 ] ) j ++ ; i ++ ; } } return ( j == lenText ) ; } public static void Main ( ) { String str = " ABABABA " ; String pat = " A # B # A " ; if ( regexMatch ( str , pat ) ) Console . Write ( " yes " ) ; else Console . Write ( " no " ) ; } }
Replace all occurrences of string AB with C without using extra space | Efficient C # program to replace all occurrences of " AB " with " C " ; Index in modified string ; Index in original string ; Traverse string ; Replace occurrence of " AB " with " C " ; Increment j by 2 ; add a null character to terminate string ; Driver code
using System ; class GFG { static void translate ( char [ ] str ) { int len = str . Length ; if ( len < 2 ) return ; int i = 0 ; int j = 0 ; while ( j < len - 1 ) { if ( str [ j ] == ' A ' && str [ j + 1 ] == ' B ' ) { j = j + 2 ; str [ i ++ ] = ' C ' ; continue ; } str [ i ++ ] = str [ j ++ ] ; } if ( j == len - 1 ) str [ i ++ ] = str [ j ] ; str [ i ] = ' ▁ ' ; str [ len - 1 ] = ' ▁ ' ; } public static void Main ( ) { String st = " helloABworldABGfG " ; char [ ] str = st . ToCharArray ( ) ; translate ( str ) ; Console . Write ( " The ▁ modified ▁ string ▁ is ▁ : " ) ; Console . Write ( str ) ; } }