codeparrot/xlcost-text-to-code huggingface dataset:
Splitting a Numeric String | C # program to split a numeric string in an Increasing sequence if possible ; Function accepts a string and checks if string can be split . ; if there is only 1 number in the string then it is not possible to split it ; storing the substring from 0 to i + 1 to form initial number of the increasing sequence ; convert string to integer and add 1 and again convert back to string s2 ; if s2 is not a substring of number than not possible ; if s2 is the next substring of the numeric string ; Incearse num2 by 1 i . e the next number to be looked for ; check if string is fully traversed then break ; If next string doesnot occurs in a given numeric string then it is not possible ; if the string was fully traversed and conditions were satisfied ; if conditions failed to hold ; Driver Code ; Call the split function for splitting the string
using System ; class GFG { static void split ( string str ) { int len = str . Length ; if ( len == 1 ) { Console . WriteLine ( " Not ▁ Possible " ) ; return ; } string s1 = " " , s2 = " " ; long num1 , num2 ; for ( int i = 0 ; i < len / 2 ; i ++ ) { int flag = 0 ; s1 = str . Substring ( 0 , i + 1 ) ; num1 = Convert . ToInt64 ( ( s1 ) ) ; num2 = num1 + 1 ; s2 = num2 . ToString ( ) ; int k = i + 1 ; while ( flag == 0 ) { int l = s2 . Length ; if ( k + l > len ) { flag = 1 ; break ; } if ( ( str . Substring ( k , l ) . Equals ( s2 ) ) ) { flag = 0 ; num2 ++ ; k = k + l ; if ( k == len ) break ; s2 = num2 . ToString ( ) ; l = s2 . Length ; if ( k + 1 > len ) { flag = 1 ; break ; } } else flag = 1 ; } if ( flag == 0 ) { Console . WriteLine ( " Possible " + " ▁ " + s1 ) ; break ; } else if ( flag == 1 && i > len / 2 - 1 ) { Console . WriteLine ( " Not ▁ Possible " ) ; break ; } } } static void Main ( ) { string str = "99100" ; split ( str ) ; } }
Find minimum shift for longest common prefix | C # program to find longest common prefix after rotation of second string . ; function for KMP search ; p [ 1 ] = 0 ; ; find out the longest prefix and position ; for new position with longer prefix in str2 update pos and len ; print result ; Driver code
using System ; class GFG { static void KMP ( int m , int n , String str2 , String str1 ) { int pos = 0 , len = 0 ; int [ ] p = new int [ m + 1 ] ; int k = 0 ; char [ ] ch1 = str1 . ToCharArray ( ) ; char [ ] ch2 = str2 . ToCharArray ( ) ; for ( int i = 2 ; i <= n ; i ++ ) { while ( k > 0 && ch1 [ k ] != ch1 [ i - 1 ] ) k = p [ k ] ; if ( ch1 [ k ] == ch1 [ i - 1 ] ) ++ k ; p [ i ] = k ; } for ( int j = 0 , i = 0 ; i < m ; i ++ ) { while ( j > 0 && j < n && ch1 [ j ] != ch2 [ i ] ) j = p [ j ] ; if ( j < n && ch1 [ j ] == ch2 [ i ] ) j ++ ; if ( j > len ) { len = j ; pos = i - j + 1 ; } } Console . WriteLine ( " Shift ▁ = ▁ " + pos ) ; Console . WriteLine ( " Prefix ▁ = ▁ " + str1 . Substring ( 0 , len ) ) ; } public static void Main ( String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " forgeeksgeeks " ; int n = str1 . Length ; str2 = str2 + str2 ; KMP ( 2 * n , n , str2 , str1 ) ; } }
Find all the patterns of "1(0 + ) 1" in a given string | SET 1 ( General Approach ) | C # Code to count 1 ( 0 + ) 1 patterns in a string ; Function to count patterns ; Variable to store the last character ; We found 0 and last character was '1' , state change ; After the stream of 0 ' s , ▁ we ▁ ▁ got ▁ a ▁ ' 1 ',counter incremented ; Last character stored ; Driver Code
using System ; class GFG { static int patternCount ( String str ) { char last = str [ 0 ] ; int i = 1 , counter = 0 ; while ( i < str . Length ) { if ( str [ i ] == '0' && last == '1' ) { while ( str [ i ] == '0' ) i ++ ; if ( str [ i ] == '1' ) counter ++ ; } last = str [ i ] ; i ++ ; } return counter ; } public static void Main ( ) { String str = "1001ab010abc01001" ; Console . Write ( patternCount ( str ) ) ; } }
Boyer Moore Algorithm | Good Suffix heuristic | C # program for Boyer Moore Algorithm with Good Suffix heuristic to find pattern in given text string ; preprocessing for strong good suffix rule ; m is the length of pattern ; if character at position i - 1 is not equivalent to character at j - 1 , then continue searching to right of the pattern for border ; the character preceding the occurrence of t in pattern P is different than the mismatching character in P , we stop skipping the occurrences and shift the pattern from i to j ; Update the position of next border ; p [ i - 1 ] matched with p [ j - 1 ] , border is found . store the beginning position of border ; Preprocessing for case 2 ; set the border position of the first character of the pattern to all indices in array shift having shift [ i ] = 0 ; suffix becomes shorter than bpos [ 0 ] , use the position of next widest border as value of j ; Search for a pattern in given text using Boyer Moore algorithm with Good suffix rule ; s is shift of the pattern with respect to text ; initialize all occurrence of shift to 0 ; do preprocessing ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at the current shift , then index j will become - 1 after the above loop ; pat [ i ] != pat [ s + j ] so shift the pattern shift [ j + 1 ] times ; Driver Code
using System ; class GFG { static void preprocess_strong_suffix ( int [ ] shift , int [ ] bpos , char [ ] pat , int m ) { int i = m , j = m + 1 ; bpos [ i ] = j ; while ( i > 0 ) { while ( j <= m && pat [ i - 1 ] != pat [ j - 1 ] ) { if ( shift [ j ] == 0 ) shift [ j ] = j - i ; j = bpos [ j ] ; } i -- ; j -- ; bpos [ i ] = j ; } } static void preprocess_case2 ( int [ ] shift , int [ ] bpos , char [ ] pat , int m ) { int i , j ; j = bpos [ 0 ] ; for ( i = 0 ; i <= m ; i ++ ) { if ( shift [ i ] == 0 ) shift [ i ] = j ; if ( i == j ) j = bpos [ j ] ; } } static void search ( char [ ] text , char [ ] pat ) { int s = 0 , j ; int m = pat . Length ; int n = text . Length ; int [ ] bpos = new int [ m + 1 ] ; int [ ] shift = new int [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) shift [ i ] = 0 ; preprocess_strong_suffix ( shift , bpos , pat , m ) ; preprocess_case2 ( shift , bpos , pat , m ) ; while ( s <= n - m ) { j = m - 1 ; while ( j >= 0 && pat [ j ] == text [ s + j ] ) j -- ; if ( j < 0 ) { Console . Write ( " pattern ▁ occurs ▁ at ▁ shift ▁ = ▁ { 0 } STRNEWLINE " , s ) ; s += shift [ 0 ] ; } else s += shift [ j + 1 ] ; } } public static void Main ( String [ ] args ) { char [ ] text = " ABAAAABAACD " . ToCharArray ( ) ; char [ ] pat = " ABA " . ToCharArray ( ) ; search ( text , pat ) ; } }
Maximum length prefix of one string that occurs as subsequence in another | C # program to find maximum length prefix of one string occur as subsequence in another string . ; Return the maximum length prefix which is subsequence . ; Iterating string T . ; If end of string S . ; If character match , increment counter . ; Driver Code
using System ; class GFG { static int maxPrefix ( String s , String t ) { int count = 0 ; for ( int i = 0 ; i < t . Length ; i ++ ) { if ( count == s . Length ) break ; if ( t [ i ] == s [ count ] ) count ++ ; } return count ; } public static void Main ( ) { String S = " digger " ; String T = " biggerdiagram " ; Console . Write ( maxPrefix ( S , T ) ) ; } }
Replace all occurrences of string AB with C without using extra space | C # program to replace all occurrences of " AB " with " C " ; Start traversing from second character ; If previous character is ' A ' and current character is 'B" ; Replace previous character with ' C ' and move all subsequent characters one position back ; Driver code
using System ; class GFG { static void translate ( char [ ] str ) { for ( int i = 1 ; i < str . Length ; i ++ ) { if ( str [ i - 1 ] == ' A ' && str [ i ] == ' B ' ) { str [ i - 1 ] = ' C ' ; int j ; for ( j = i ; j < str . Length - 1 ; j ++ ) str [ j ] = str [ j + 1 ] ; str [ j ] = ' ▁ ' ; } } return ; } public static void Main ( ) { String st = " helloABworldABGfG " ; char [ ] str = st . ToCharArray ( ) ; translate ( str ) ; Console . WriteLine ( " The ▁ modified ▁ string ▁ is ▁ : " ) ; Console . Write ( str ) ; } }
Find all occurrences of a given word in a matrix | C # Program to find all occurrences of the word in a matrix ; check whether given cell ( row , col ) is a valid cell or not . ; return true if row number and column number is in range ; These arrays are used to get row and column numbers of 8 neighboursof a given cell ; A utility function to do DFS for a 2D bool matrix . It only considers the 8 neighbours as adjacent vertices ; return if current character doesn 't match with  the next character in the word ; append current character position to path ; current character matches with the last character in the word ; Recur for all connected neighbours ; The main function to find all occurrences of the word in a matrix ; traverse through the all cells of given matrix ; occurrence of first character in matrix ; check and print if path exists ; Driver code
using System ; class GFG { static readonly int ROW = 3 ; static readonly int COL = 5 ; static bool isvalid ( int row , int col , int prevRow , int prevCol ) { return ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ! ( row == prevRow && col == prevCol ) ; } static int [ ] rowNum = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; static int [ ] colNum = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; static void DFS ( char [ , ] mat , int row , int col , int prevRow , int prevCol , char [ ] word , String path , int index , int n ) { if ( index > n mat [ row , col ] != word [ index ] ) return ; path += ( word [ index ] ) + " ( " + String . Join ( " " , row ) + " , ▁ " + String . Join ( " " , col ) + " ) ▁ " ; if ( index == n ) { Console . Write ( path + " STRNEWLINE " ) ; return ; } for ( int k = 0 ; k < 8 ; ++ k ) if ( isvalid ( row + rowNum [ k ] , col + colNum [ k ] , prevRow , prevCol ) ) DFS ( mat , row + rowNum [ k ] , col + colNum [ k ] , row , col , word , path , index + 1 , n ) ; } static void findWords ( char [ , ] mat , char [ ] word , int n ) { for ( int i = 0 ; i < ROW ; ++ i ) for ( int j = 0 ; j < COL ; ++ j ) if ( mat [ i , j ] == word [ 0 ] ) DFS ( mat , i , j , - 1 , - 1 , word , " " , 0 , n ) ; } public static void Main ( String [ ] args ) { char [ , ] mat = { { ' B ' , ' N ' , ' E ' , ' Y ' , ' S ' } , { ' H ' , ' E ' , ' D ' , ' E ' , ' S ' } , { ' S ' , ' G ' , ' N ' , ' D ' , ' E ' } } ; char [ ] word = " DES " . ToCharArray ( ) ; findWords ( mat , word , word . Length - 1 ) ; } }
Maximum length of string formed by concatenation having even frequency of each character | C # Implementation of the above approach ; Function to check the string ; Count the frequency of the string ; Check the frequency of the string ; Store the Length of the new String ; Function to find the longest concatenated string having every character of even frequency ; Checking the string ; Dont Include the string ; Include the string ; Driver code ; Call the function ; Print the answer
using System ; public class index { static int max = 0 ; static String ans1 = " " ; static void calculate ( String ans ) { int [ ] dp = new int [ 26 ] ; for ( int i = 0 ; i < ans . Length ; ++ i ) { dp [ ( int ) ans [ i ] - ( int ) ' A ' ] ++ ; } for ( int i = 0 ; i < dp . Length ; ++ i ) { if ( dp [ i ] % 2 == 1 ) { return ; } } if ( max < ans . Length ) { max = ans . Length ; ans1 = ans ; } } static void longestString ( String [ ] arr , int index , String str ) { if ( index == arr . Length ) { return ; } longestString ( arr , index + 1 , str ) ; str += arr [ index ] ; calculate ( str ) ; longestString ( arr , index + 1 , str ) ; } public static void Main ( ) { String [ ] A = { " ABAB " , " ABF " , " CDA " , " AD " , " CCC " } ; longestString ( A , 0 , " " ) ; Console . WriteLine ( ans1 + " ▁ " + ans1 . Length ) ; } }
Find the next number by adding natural numbers in order on alternating indices from last | C # program for the above approach ; Function to generate the resultant number using the given criteria ; Storing end result ; Find the length of numeric string ; Traverse the string ; Storing digit at ith position ; Checking that the number would be added or not ; Logic for summing the digits if the digit is greater than 10 ; Storing the result ; Returning the result ; Driver Code
using System ; public class GFG { public static String generateNumber ( string number ) { int temp = 0 , adding_number = 0 ; string result = " " ; int len = number . Length ; for ( int i = len - 1 ; i >= 0 ; i -- ) { int digit = ( int ) number [ i ] - ( int ) '0' ; if ( temp % 2 == 0 ) { adding_number += 1 ; digit += adding_number ; if ( digit >= 10 ) { digit %= 9 ; if ( digit == 0 ) digit = 9 ; } } result = digit + result ; temp += 1 ; } return result ; } public static void Main ( string [ ] args ) { string S = "1345" ; Console . WriteLine ( generateNumber ( S ) ) ; } }
Find the single digit sum of alphabetical values of a string | C # program for the above approach ; Traverse the given string ; If character is an alphabet ; Stores the sum of order of values ; Find the score ; Find the single digit sum ; Return the resultant sum ; Driver Code
using System ; class GFG { static int findTheSum ( string str ) { string alpha = " " ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) ) alpha += ( str [ i ] ) ; } int score = 0 , n = 0 ; for ( int i = 0 ; i < alpha . Length ; i ++ ) { if ( alpha [ i ] >= ' A ' && alpha [ i ] <= ' Z ' ) score += alpha [ i ] - ' A ' + 1 ; else score += alpha [ i ] - ' a ' + 1 ; } while ( score > 0 n > 9 ) { if ( score == 0 ) { score = n ; n = 0 ; } n += score % 10 ; score /= 10 ; } return n ; } public static void Main ( ) { string S = " GeeksforGeeks " ; Console . WriteLine ( findTheSum ( S ) ) ; } }
Count of distinct groups of strings formed after performing equivalent operation | C # program for the above approach ; Function to perform the find operation to find the parent of a disjoint set ; Function to perform union operation of disjoint set union ; Find the parent of node a and b ; Update the rank ; Function to find the number of distinct strings after performing the given operations ; Stores the parent elements of the sets ; Stores the rank of the sets ; Update parent [ i ] to i ; Stores the total characters traversed through the strings ; Stores the current characters traversed through a string ; Update current [ i ] to false ; Update current [ ch - ' a ' ] to true ; Check if current [ j ] is true ; Update total [ j ] to true ; Add arr [ i ] [ 0 ] - ' a ' and j elements to same set ; Stores the count of distinct strings ; Check total [ i ] is true and parent of i is i only ; Increment the value of distCount by 1 ; Print the value of distCount ; Driver Code
using System ; class GFG { static int Find ( int [ ] parent , int a ) { return parent [ a ] = ( parent [ a ] == a ? a : Find ( parent , parent [ a ] ) ) ; } static void Union ( int [ ] parent , int [ ] rank , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( rank [ a ] == rank [ b ] ) rank [ a ] ++ ; if ( rank [ a ] > rank [ b ] ) parent [ b ] = a ; else parent [ a ] = b ; } static void numOfDistinctStrings ( string [ ] arr , int N ) { int [ ] parent = new int [ ( 27 ) ] ; int [ ] rank = new int [ ( 27 ) ] ; for ( int j = 0 ; j < 27 ; j ++ ) { parent [ j ] = j ; } bool [ ] total = new bool [ 26 ] ; bool [ ] current = new bool [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { current [ j ] = false ; } foreach ( char ch in arr [ i ] ) { current [ ch - ' a ' ] = true ; } for ( int j = 0 ; j < 26 ; j ++ ) { if ( current [ j ] ) { total [ j ] = true ; Union ( parent , rank , arr [ i ] [ 0 ] - ' a ' , j ) ; } } } int distCount = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( total [ i ] && Find ( parent , i ) == i ) { distCount ++ ; } } Console . WriteLine ( distCount ) ; } public static void Main ( ) { string [ ] arr = { " a " , " ab " , " b " , " d " } ; int N = arr . Length ; numOfDistinctStrings ( arr , N ) ; } }
Minimum adjacent swaps required to make a binary string alternating | C # program for the above approach ; Function to find the minimum number of adjacent swaps to make the String alternating ; Count the no of zeros and ones ; Base Case ; Store no of min swaps when String starts with "1" ; Keep track of the odd positions ; Checking for when the String starts with "1" ; Adding the no of swaps to fix "1" at odd positions ; Store no of min swaps when String starts with "0" ; Keep track of the odd positions ; Checking for when the String starts with "0" ; Adding the no of swaps to fix "1" at odd positions ; Returning the answer based on the conditions when String length is even ; When String length is odd ; When no of ones is greater than no of zeros ; When no of ones is greater than no of zeros ; Driver Code
using System ; class GFG { static int minSwaps ( String s ) { int ones = 0 , zeros = 0 ; int N = s . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) ones ++ ; else zeros ++ ; } if ( ( N % 2 == 0 && ones != zeros ) || ( N % 2 == 1 && Math . Abs ( ones - zeros ) != 1 ) ) { return - 1 ; } int ans_1 = 0 ; int j = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) { ans_1 += Math . Abs ( j - i ) ; j += 2 ; } } int ans_0 = 0 ; int k = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '0' ) { ans_0 += Math . Abs ( k - i ) ; k += 2 ; } } if ( N % 2 == 0 ) return Math . Min ( ans_1 , ans_0 ) ; else { if ( ones > zeros ) return ans_1 ; else return ans_0 ; } } public static void Main ( ) { String S = "110100" ; Console . WriteLine ( minSwaps ( S ) ) ; } }
Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time | C # program for the above approach ; Function to find all the ways to reach Nth stair using one or two jumps ; Base Cases ; Recur for jump1 and jump2 ; Stores the total possible jumps ; Add "1" with every element present in jump1 ; Add "2" with every element present in jump2 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < string > TotalPossibleJumps ( int N ) { if ( ( N - 1 ) == 0 ) { List < string > newvec = new List < string > ( ) ; newvec . Add ( " " ) ; return newvec ; } else { if ( N < 0 ) { List < string > newvec = new List < string > ( ) ; return newvec ; } } List < string > jump1 = TotalPossibleJumps ( N - 1 ) ; List < string > jump2 = TotalPossibleJumps ( N - 2 ) ; List < string > totaljumps = new List < string > ( ) ; foreach ( string s in jump1 ) { totaljumps . Add ( "1" + s ) ; } foreach ( string s in jump2 ) { totaljumps . Add ( "2" + s ) ; } return totaljumps ; } public static void Main ( ) { int N = 3 ; List < string > Ans = TotalPossibleJumps ( N ) ; foreach ( string it in Ans ) Console . WriteLine ( it ) ; } }
Find any permutation of Binary String of given size not present in Array | C # Program for the above approach ; Function to find a binary string of N bits that does not occur in the givrn array arr [ ] ; Stores the resultant string ; Loop to iterate over all the given strings in a diagonal order ; Append the complement of element at current index into ans ; Return Answer ; Driver code
using System ; class GFG { static string findString ( string [ ] arr , int N ) { string ans = " " ; for ( int i = 0 ; i < N ; i ++ ) { ans += arr [ i ] [ i ] == '0' ? '1' : '0' ; } return ans ; } public static void Main ( String [ ] args ) { string [ ] arr = { "111" , "011" , "001" } ; int N = arr . Length ; Console . WriteLine ( findString ( arr , N ) ) ; } }
Check if the string has a reversible equal substring at the ends | C # program for the above approach ; Function to print longest substring that appears at beginning of string and also at end in reverse order ; Stores the resultant string ; If the characters are same ; Otherwise , break ; If the string can 't be formed ; Otherwise print resultant string ; Driver Code
using System ; class GFG { static void commonSubstring ( string s ) { int n = s . Length ; int i = 0 ; int j = n - 1 ; string ans = " " ; while ( j >= 0 ) { if ( s [ i ] == s [ j ] ) { ans += s [ i ] ; i ++ ; j -- ; } else { break ; } } if ( ans . Length == 0 ) Console . WriteLine ( " False " ) ; else { Console . WriteLine ( " True ▁ " ) ; Console . WriteLine ( ans ) ; } } public static void Main ( ) { string S = " abca " ; commonSubstring ( S ) ; } }
Length of Smallest Non Prime Subsequence in given numeric String | C # program for the above approach ; Function to find the smallest length of resultant subsequence ; Check for a subsequence of size 1 ; Check for a subsequence of size 2 ; If none of the above check is successful then subsequence must be of size 3 ; Never executed ; Driver Code
using System ; class GFG { static void findMinimumSubsequence ( string S ) { bool flag = false ; string dummy = " " ; for ( int j = 0 ; j < S . Length ; j ++ ) { if ( S [ j ] != '2' && S [ j ] != '3' && S [ j ] != '5' && S [ j ] != '7' ) { Console . WriteLine ( 1 ) ; flag = true ; break ; } } if ( ! flag ) { for ( int j = 0 ; j < S . Length - 1 ; j ++ ) { for ( int j1 = j + 1 ; j1 < S . Length ; j1 ++ ) { dummy = S [ j ] . ToString ( ) + S [ j1 ] . ToString ( ) ; if ( dummy != "23" && dummy != "37" && dummy != "53" && dummy != "73" ) { Console . WriteLine ( 2 ) ; } if ( flag == true ) break ; else flag = true ; } if ( flag == true ) break ; } } if ( flag == false ) { if ( S . Length >= 3 ) { Console . WriteLine ( 3 ) ; } else { Console . WriteLine ( - 1 ) ; } } } public static void Main ( ) { string S = "237" ; findMinimumSubsequence ( S ) ; } }
Count of ways to empty given String by recursively removing all adjacent duplicates | C # implementation for the above approach ; Define the dp table globally ; Recursive function to calculate the dp values for range [ L , R ] ; The range is odd length ; The state is already calculated ; If the length is 2 ; Total answer for this state ; Variable to store the current answer . ; Remove characters s [ l ] and s [ i ] . ; Initialize all the states of dp to - 1 ; Calculate all Combinations ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int [ , ] dp = new int [ 505 , 505 ] ; static int [ , ] choose = new int [ 502 , 502 ] ; static int calc ( int l , int r , string s ) { if ( Math . Abs ( r - l ) % 2 == 0 ) { return 0 ; } if ( l > r ) { return dp [ l , r ] = 1 ; } if ( dp [ l , r ] != - 1 ) { return dp [ l , r ] ; } if ( ( r - l ) == 1 ) { if ( s [ l ] == s [ r ] ) { dp [ l , r ] = 1 ; } else { dp [ l , r ] = 0 ; } return dp [ l , r ] ; } int ans = 0 ; for ( int k = l + 1 ; k <= r ; k += 2 ) { int temp = 1 ; if ( s [ l ] == s [ k ] ) { temp = calc ( l + 1 , k - 1 , s ) * calc ( k + 1 , r , s ) * choose [ ( r - l + 1 ) / 2 , ( r - k ) / 2 ] ; ans += temp ; } } return dp [ l , r ] = ans ; } static int waysToClearString ( string S ) { for ( int i = 0 ; i < 505 ; i ++ ) { for ( int j = 0 ; j < 505 ; j ++ ) dp [ i , j ] = - 1 ; } int n = S . Length ; choose [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= n / 2 ; ++ i ) { choose [ i , 0 ] = 1 ; for ( int j = 1 ; j <= i ; ++ j ) { choose [ i , j ] = ( choose [ i - 1 , j ] + choose [ i - 1 , j - 1 ] ) ; } } return calc ( 0 , n - 1 , S ) ; } public static void Main ( ) { string S = " aabccb " ; Console . Write ( waysToClearString ( S ) ) ; } }