codeparrot/xlcost-text-to-code huggingface dataset:
Rabin | Following program is a C # implementation of Rabin Karp Algorithm given in the CLRS book ; d is the number of characters in the input alphabet ; pat -> pattern txt -> text q -> A prime number ; hash value for pattern int t = 0 ; hash value for txt ; The value of h would be " pow ( d , ▁ M - 1 ) % q " ; Calculate the hash value of pattern and first window of text ; Slide the pattern over text one by one ; Check the hash values of current window of text and pattern . If the hash values match then only check for characters on by one ; Check for characters one by one ; if p == t and pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Calculate hash value for next window of text : Remove leading digit , add trailing digit ; We might get negative value of t , converting it to positive ; Driver Code ; A prime number ; Function Call
using System ; public class GFG { public readonly static int d = 256 ; static void search ( String pat , String txt , int q ) { int M = pat . Length ; int N = txt . Length ; int i , j ; int h = 1 ; for ( i = 0 ; i < M - 1 ; i ++ ) h = ( h * d ) % q ; for ( i = 0 ; i < M ; i ++ ) { p = ( d * p + pat [ i ] ) % q ; t = ( d * t + txt [ i ] ) % q ; } for ( i = 0 ; i <= N - M ; i ++ ) { if ( p == t ) { for ( j = 0 ; j < M ; j ++ ) { if ( txt [ i + j ] != pat [ j ] ) break ; } if ( j == M ) Console . WriteLine ( " Pattern ▁ found ▁ at ▁ index ▁ " + i ) ; } if ( i < N - M ) { t = ( d * ( t - txt [ i ] * h ) + txt [ i + M ] ) % q ; if ( t < 0 ) t = ( t + q ) ; } } } public static void Main ( ) { String txt = " GEEKS ▁ FOR ▁ GEEKS " ; String pat = " GEEK " ; int q = 101 ; search ( pat , txt , q ) ; } }
Check if count of 1 s can be made greater in a Binary string by changing 0 s adjacent to 1 s | C # program for the above approach ; Function to check whether in a given binary string can we make number of 1 ' s ▁ greater ▁ than ▁ the ▁ number ▁ of ▁ 0' s by doing the given operation ; Stores the count of 0 's ; Stores the count of 1 's ; Traverse through the string S ; Check current character is 1 ; Update cnt1 ; Update cnt0 ; Traverse through the string S ; Check curretn character is 1 ; Check if left adjacent character is 0 ; Change the left adjacent character to _ ; Update the cnt0 ; Check if right adjacent character is 0 ; Change the right adjacent character to _ ; Update the cnt0 ; Check count of 1 ' s ▁ is ▁ greater ▁ ▁ than ▁ the ▁ count ▁ of ▁ 0' s ; Driver Code
using System ; using System . Text ; class GFG { static void isOnesGreater ( string S , int N ) { StringBuilder st = new StringBuilder ( S ) ; int cnt0 = 0 ; int cnt1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( st [ i ] == '1' ) cnt1 ++ ; else cnt0 ++ ; } for ( int i = 0 ; i < N ; i ++ ) { if ( st [ i ] == '1' ) { if ( i > 0 && st [ i - 1 ] == '0' ) { st [ i - 1 ] = ' _ ' ; cnt0 -- ; } else if ( i < N && st [ i + 1 ] == '0' ) { st [ i + 1 ] = ' _ ' ; cnt0 -- ; } } } if ( cnt1 > cnt0 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( ) { string S = "01" ; int N = S . Length ; isOnesGreater ( S , N ) ; } }
Minimize flips to make binary string as all 1 s by flipping characters in substring of size K repeatedly | C # program for the above approach ; Function to find the minimum number of operations required to convert all the characters to 1 by flipping the substrings of size K ; Stores the minimum number of operations required ; Traverse the string S ; If the character is 0 ; Flip the substrings of size K starting from i ; Increment the minimum count of operations required ; After performing the operations check if string S contains any 0 s ; If S contains only 1 's ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void minOperation ( string S , int K , int N ) { int min = 0 ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '0' && i + K <= N ) { for ( int j = i ; j < i + K ; j ++ ) { if ( S [ j ] == '1' ) S = S . Substring ( 0 , j ) + '0' + S . Substring ( j + 1 ) ; else S = S . Substring ( 0 , j ) + '1' + S . Substring ( j + 1 ) ; } min ++ ; } } for ( i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '0' ) break ; } if ( i == N ) Console . Write ( min ) ; else Console . Write ( - 1 ) ; } public static void Main ( ) { string S = "00010110" ; int K = 3 ; int N = S . Length ; minOperation ( S , K , N ) ; } }
Check if string S1 can be formed using repeated insertions of another string S2 | C # implementation for the above approach ; Function to check a valid insertion ; Store the size of string ; Maintain a stack for characters ; Iterate through the string ; push the current character on top of the stack ; If the current character is the last character of string S2 then pop characters until S2 is not formed ; index of last character of the string S2 ; pop characters till 0 - th index ; Check if stack in non - empty ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool validInsertionstring ( string S1 , string S2 ) { int N = S1 . Length ; int M = S2 . Length ; Stack < char > st = new Stack < char > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . Push ( S1 [ i ] ) ; if ( S1 [ i ] == S2 [ M - 1 ] ) { int idx = M - 1 ; while ( idx >= 0 ) { if ( st . Count == 0 ) { return false ; } char c = st . Peek ( ) ; st . Pop ( ) ; if ( c != S2 [ idx ] ) { return false ; } idx -- ; } } } if ( st . Count > 0 ) { return false ; } else { return true ; } } public static void Main ( ) { string S1 = " aabb " ; string S2 = " ab " ; if ( validInsertionstring ( S1 , S2 ) == true ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if rearranging Array elements can form a Palindrome or not | C # implementation of the above approach ; Function to check whether elements of an array can form a palindrome ; create an empty string to append elements of an array ; append each element to the string str to form a string so that we can solve it in easy way ; Create a freq array and initialize all values as 0 ; For each character in formed string , increment freq in the corresponding freq array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Drive code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 256 ; static bool can_form_palindrome ( int [ ] arr , int n ) { string str = " " ; for ( int i = 0 ; i < n ; i ++ ) { str += arr [ i ] ; } int [ ] freq = new int [ MAX ] ; Array . Clear ( freq , 0 , MAX ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { freq [ str [ i ] ] ++ ; } int count = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( ( freq [ i ] & 1 ) != 0 ) { count ++ ; } if ( count > 1 ) { return false ; } } return true ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 , 2 } ; int n = arr . Length ; if ( can_form_palindrome ( arr , n ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Minimum number of flips to make a Binary String increasing | C # program for the above approach ; ; Function to find the minimum number of flips required to make String increasing ; Length of s ; Total number of zero in s ; Stores count of 1 s till ith index ; stors the minimum count of flip ; Traverse the given String ; Update the value of res and count of 1 s ; Return the minimum number of flips ; Utility count ; Driver code ; Given String ; function call
using System ; public class GFG { static int minimumFlips ( String s ) { int n = s . Length ; int cnt0 = count ( s , '0' ) ; int cnt1 = 0 ; int res = n - cnt0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) { cnt0 -= 1 ; } else if ( s [ i ] == '1' ) { res = Math . Min ( res , cnt1 + cnt0 ) ; cnt1 ++ ; } } return res ; } private static int count ( String s , char c ) { int ans = 0 ; for ( int j = 0 ; j < s . Length ; j ++ ) { char i = s [ j ] ; if ( c == i ) ans ++ ; } return ans ; } static public void Main ( ) { String S = "000110" ; Console . Write ( minimumFlips ( S ) ) ; } }
Lexicographically shortest string of length at most K which is not a substring of given String | C # implementation for above approach ; Function to return a set of all substrings of given string which have length less than or equal to k ; Function to print the lexicographically smallest substring of length atmost k which is not present in given string s ; All substrings of length atmost k present in string s are stored in this set ; Loop to change length of substring ; String with length = len which has all characters as ' a ' ; If the substrings set does not contain this string then we have found the answer ; Changing the likes of ' azz ' and ' daz ' to ' baa ' and ' dba ' respectively ; Reached a string like ' zz ' or ' zzz ' increase the length of the substring ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static HashSet < string > presentSubstring ( string s , int k ) { HashSet < string > st = new HashSet < string > ( ) ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { string s1 = " " ; for ( int j = 0 ; j < k && i + j < n ; j ++ ) { s1 += s [ i + j ] ; st . Add ( s1 ) ; } } return st ; } static string smallestSubstring ( string s , int k ) { HashSet < string > st = new HashSet < string > ( ) ; st = presentSubstring ( s , k ) ; int index ; for ( int len = 1 ; len <= k ; len ++ ) { string t = " " ; for ( int i = 0 ; i < len ; i ++ ) t += ' a ' ; while ( true ) { if ( st . Contains ( t ) == false ) { return t ; } index = len - 1 ; while ( index >= 0 && t [ index ] == ' z ' ) { t = t . Substring ( 0 , index ) + ' a ' + t . Substring ( index + 1 ) ; index -- ; } if ( index >= 0 ) { t = t . Substring ( 0 , index ) + Convert . ToChar ( ( int ) t [ index ] + 1 ) + t . Substring ( index + 1 ) ; } else break ; } public static void Main ( ) { string s = " sdhaacbdefghijklmnopqrstuvwxyz " ; int K = 3 ; Console . Write ( smallestSubstring ( s , K ) ) ; } }
Maximum length of a substring required to be flipped repeatedly to make all characters of binary string equal to 0 | C # code for the above approach ; Function to find the maximum value of K such that flipping substrings of size at least K make all characters 0 s ; Stores the maximum value of K ; Traverse the given string S ; Store the minimum of the maximum of LHS and RHS length ; Flip performed ; If no flips performed ; Return the possible value of K ; Driver Code
using System ; public class GFG { static int maximumK ( String S ) { int N = S . Length ; int ans = N ; int flag = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( S [ i ] != S [ i + 1 ] ) { flag = 1 ; ans = Math . Min ( ans , Math . Max ( i + 1 , N - i - 1 ) ) ; } } if ( flag == 0 ) return 0 ; return ans ; } static public void Main ( ) { string S = "010" ; Console . Write ( maximumK ( S ) ) ; } }
Minimum moves to make count of lowercase and uppercase letters equal | C # program for the above approach ; Function to calculate minimum number of moves required to convert the string ; Stores Count of upper and lower case characters ; Traverse the string S ; If current character is uppercase ; Increment count of Uppercase characters ; Otherwise , ; Increment count of Lowercase characters ; Stores minimum number of moves needed ; If there are more upper case characters ; Iterate until upper is greater than N / 2 ; Convert uppercase into lowercase until upper = N / 2 ; Increment the pointer ; If there are more lower case characters ; Iterate until lower is greater than N / 2 ; Convert lowercase into uppercase until lower = N / 2 ; Increment the pointer ; Print moves required ; Print resultant string ; Given string ; Function call
using System ; class GFG { static void minimumTimeToConvertString ( char [ ] S , int N ) { int upper = 0 , lower = 0 ; for ( int i = 0 ; i < N ; i ++ ) { char c = S [ i ] ; if ( Char . IsUpper ( c ) ) { upper ++ ; } else { lower ++ ; } } int moves = 0 ; if ( upper > N / 2 ) { int i = 0 ; while ( upper > N / 2 && i < N ) { if ( Char . IsUpper ( S [ i ] ) ) { S [ i ] += ( char ) 32 ; moves ++ ; upper -- ; lower ++ ; } i ++ ; } } else if ( lower > N / 2 ) { int i = 0 ; while ( lower > N / 2 && i < N ) { if ( Char . IsLower ( S [ i ] ) ) { S [ i ] = ( char ) ( ( int ) S [ i ] - 32 ) ; moves ++ ; upper ++ ; lower -- ; } i ++ ; } } Console . WriteLine ( moves ) ; Console . WriteLine ( new string ( S ) ) ; } static void Main ( ) { string S = " AbcdEf " ; int N = S . Length ; minimumTimeToConvertString ( S . ToCharArray ( ) , N ) ; } }
Program to print a string in vertical zigzag manner | C # program for the above approach ; Function to print any string in zigzag fashion ; Store the gap between the major columns ; Traverse through rows ; Store the step value for each row ; Iterate in the range [ 1 , N - 1 ] ; Print the character ; Print the spaces before character s [ j + step ] ; Print the character ; Print the spaces after character after s [ j + step ] ; Print the spaces for first and last rows ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void zigzag ( string s , int rows ) { int interval = 2 * rows - 2 ; for ( int i = 0 ; i < rows ; i ++ ) { int step = interval - 2 * i ; for ( int j = i ; j < s . Length ; j = j + interval ) { Console . Write ( s [ j ] ) ; if ( step > 0 && step < interval && step + j < s . Length ) { for ( int k = 0 ; k < ( interval - rows - i ) ; k ++ ) Console . Write ( " ▁ " ) ; Console . Write ( s [ j + step ] ) ; for ( int k = 0 ; k < i - 1 ; k ++ ) Console . Write ( " ▁ " ) ; } else { for ( int k = 0 ; k < ( interval - rows ) ; k ++ ) Console . Write ( " ▁ " ) ; } } Console . WriteLine ( ) ; } } public static void Main ( ) { string s = "123456789ABCDEFGHIJKLM " + " NOPQRSTUVWXYZabcdefghi " + " jklmnopqrstuvwxyz " ; int rows = 9 ; zigzag ( s , rows ) ; } }
Number of substrings with each character occurring even times | C # program for the above approach ; Function to count substrings having even frequency of each character ; Stores the count of a character ; Stores bitmask ; Stores the count of substrings with even count of each character ; Traverse the string S ; Flip the ord ( i ) - 97 bits in pre ; Increment the count by hash [ pre ] ; Increment count of pre in hash ; Return the total count obtained ; Driver code
using System . IO ; using System ; using System . Collections . Generic ; class GFG { static int subString ( string s , int n ) { Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; hash [ 0 ] = 1 ; int pre = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { pre ^= ( 1 << ( int ) ( s [ i ] ) - 97 ) ; if ( hash . ContainsKey ( pre ) ) count += hash [ pre ] ; else count += 0 ; if ( hash . ContainsKey ( pre ) ) hash [ pre ] = hash [ pre ] + 1 ; else hash . Add ( pre , 1 ) ; } return count ; } static void Main ( ) { String S = " abbaa " ; int N = S . Length ; Console . WriteLine ( subString ( S , N ) ) ; } }
Count number of substrings having at least K distinct characters | C # program for the above approach ; Function to count number of substrings having atleast k distinct characters ; Stores the size of the string ; Initialize a HashMap ; Stores the start and end indices of sliding window ; Stores the required result ; Iterate while the end pointer is less than n ; Include the character at the end of the window ; Increment end pointer by 1 ; Iterate until count of distinct characters becomes less than K ; Remove the character from the beginning of window ; If its frequency is 0 , remove it from the map ; Update the answer ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void atleastkDistinctChars ( string s , int k ) { int n = s . Length ; Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; int begin = 0 , end = 0 ; int ans = 0 ; while ( end < n ) { char c = s [ end ] ; if ( mp . ContainsKey ( c ) ) mp ++ ; else mp . Add ( c , 1 ) ; end ++ ; while ( mp . Count >= k ) { char pre = s [ begin ] ; mp [ pre ] -- ; if ( mp [ pre ] == 0 ) { mp . Remove ( pre ) ; } ans += s . Length - end + 1 ; begin ++ ; } } Console . Write ( ans ) ; } public static void Main ( ) { string S = " abcca " ; int K = 3 ; atleastkDistinctChars ( S , K ) ; } }
Rearrange characters of a string to make it a concatenation of palindromic substrings | C # program for the above approach ; Function to check if a string can be modified such that it can be split into palindromic substrings of length >= 2 ; Stores frequencies of characters ; Traverse the string ; Update frequency of each character ; Traverse the frequency array ; Update values of odd and eve ; Print the result ; Driver Code
using System ; public class GFG { static void canSplit ( string S ) { int [ ] frequency = new int [ 26 ] ; int cnt_singles = 0 ; int k = 0 ; for ( int i = 0 ; i < S . Length ; i ++ ) frequency [ S [ i ] - ' a ' ] ++ ; int odd = 0 , eve = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( frequency [ i ] != 0 ) { odd += ( frequency [ i ] & 1 ) ; eve += frequency [ i ] / 2 ; } } if ( eve >= odd ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( string [ ] args ) { string S = " aaabbbccc " ; canSplit ( S ) ; } }
Shortest string possible after removal of all pairs of similar adjacent characters | C # program for the above approach ; Function to delete pair of adjacent characters which are equal ; Base Case ; Stores the update string ; Traverse the string s ; If two unequal pair of adjacent characters are found ; If two equal pair of adjacent characters are found ; Append the remaining string after removing the pair ; Return the final String ; Function to find the shortest string after repeatedly removing pairs of adjacent characters which are equal ; Stores the resultant String ; Keeps track of previously iterated string ; Update the result after deleting adjacent pair of characters which are similar ; Termination Conditions ; Update pre variable with the value of result ; Case for " Empty ▁ String " ; Driver Code
using System ; class GFG { static string removeAdjacent ( string s ) { if ( s . Length == 1 ) return s ; string sb = " " ; for ( int i = 0 ; i < s . Length - 1 ; i ++ ) { char c = s [ i ] ; char d = s [ i + 1 ] ; if ( c != d ) { sb = sb + c ; if ( i == s . Length - 2 ) { sb = sb + d ; } } else { for ( int j = i + 2 ; j < s . Length ; j ++ ) sb = sb + s [ j ] ; return sb ; } } return sb ; } static void reduceString ( string s ) { string result = " " ; string pre = s ; while ( true ) { result = removeAdjacent ( pre ) ; if ( result == pre ) break ; pre = result ; } if ( result . Length != 0 ) Console . WriteLine ( result ) ; else Console . WriteLine ( " Empty ▁ String " ) ; } static void Main ( ) { string S = " aaabccddd " ; reduceString ( S ) ; } }
Minimum removals required such that a string can be rearranged to form a palindrome | C # program for the above approach ; Function to find the number of deletions required such that characters of the string can be rearranged to form a palindrome ; Stores frequency of characters ; Store the frequency of each character in frequency array ; Count number of characters with odd frequency ; If count is 1 or 0 , return 0 ; Otherwise , return count - 1 ; Driver Code ; Function call to find minimum number of deletions required
using System ; public class GFG { static int minDeletions ( string str ) { int [ ] fre = new int [ 26 ] ; int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { fre [ str [ i ] - ' a ' ] += 1 ; } int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % 2 == 1 ) { count += 1 ; } } if ( count == 0 count == 1 ) { return 0 ; } else { return count - 1 ; } } public static void Main ( string [ ] args ) { string str = " ababbccca " ; Console . WriteLine ( minDeletions ( str ) ) ; } }
Calculate score of parentheses from a given string | C # program for the above approach ; Function to calculate the score of the parentheses using stack ; To keep track of the score ; Initially , push 0 to stack ; Traverse the string s ; If ' ( ' is encountered , then push 0 to stack ; Otherwise ; Balance the last ' ( ' , and store the score of inner parentheses ; If tmp is not zero , it means inner parentheses exists ; Otherwise , it means no inner parentheses exists ; Pass the score of this level to parent parentheses ; Print the score ; Driver code ; Function call
using System ; using System . Collections . Generic ; public class GFG { static void scoreOfParentheses ( String s ) { Stack < int > stack = new Stack < int > ( ) ; stack . Push ( 0 ) ; foreach ( char c in s . ToCharArray ( ) ) { if ( c == ' ( ' ) stack . Push ( 0 ) ; else { int tmp = stack . Pop ( ) ; int val = 0 ; if ( tmp > 0 ) val = tmp * 2 ; else val = 1 ; stack . Push ( stack . Pop ( ) + val ) ; } } Console . WriteLine ( stack . Peek ( ) ) ; } public static void Main ( String [ ] args ) { String S = " ( ( ) ( ( ) ) ) " ; scoreOfParentheses ( S ) ; } }
Count unique substrings of a string S present in a wraparound string | C # program for the above approach ; Function to find the count of non - empty substrings of p present in s ; Stores the required answer ; Stores the length of substring present in p ; Stores the current length of substring that is present in string s starting from each character of p ; Iterate over the characters of the string ; Check if the current character can be added with previous substring to form the required substring ; Increment current length ; To avoid repetition ; Update arr [ cur ] ; Print the answer ; Driver code ; Function call to find the count of non - empty substrings of p present in s
using System ; class GFG { static void findSubstringInWraproundString ( string p ) { int ans = 0 ; int curLen = 0 ; int [ ] arr = new int [ 26 ] ; for ( int i = 0 ; i < ( int ) p . Length ; i ++ ) { int curr = p [ i ] - ' a ' ; if ( i > 0 && ( p [ i - 1 ] != ( ( curr + 26 - 1 ) % 26 + ' a ' ) ) ) { curLen = 0 ; } curLen ++ ; if ( curLen > arr [ curr ] ) { ans += ( curLen - arr [ curr ] ) ; arr [ curr ] = curLen ; } } Console . Write ( ans ) ; } static void Main ( ) { string p = " zab " ; findSubstringInWraproundString ( p ) ; } }
Minimize sum of given array by removing all occurrences of a single digit | Function to remove each digit from the given integer ; Convert into string ; Stores final string ; Traverse the string ; Append it to the final string ; Return integer value ; Function to find the minimum sum by removing occurences of each digit ; Iterate in range [ 0 , 9 ] ; Traverse the array ; Update the minimum sum ; Print the minimized sum ; Driver Code
using System ; public class GFG { static int remove ( int N , int digit ) { String strN = N . ToString ( ) ; String ans = " " ; foreach ( char i in strN . ToCharArray ( ) ) { if ( ( i - '0' ) == digit ) { continue ; } ans += i ; } return Int32 . Parse ( ans ) ; } static void getMin ( int [ ] arr ) { int minSum = Int32 . MaxValue ; for ( int i = 0 ; i < 10 ; i ++ ) { int curSum = 0 ; foreach ( int num in arr ) curSum += remove ( num , i ) ; minSum = Math . Min ( minSum , curSum ) ; } Console . WriteLine ( minSum ) ; } static public void Main ( ) { int [ ] arr = { 34 , 23 , 85 , 93 } ; getMin ( arr ) ; } }