codeparrot/xlcost-text-to-code huggingface dataset:
Generate a string of size N whose each substring of size M has exactly K distinct characters | C # program to generate a String of size N whose each subString of size M has exactly K distinct characters ; Function to generate the String ; Declare empty String ; Counter for M ; Counter for K ; Loop to generate String size of N ; Generating K distinct letters one by one ; After generating b distinct letters , append rest a - b letters as ' a ' ; Reset the counter value and repeat the process ; Return readonly result String ; Driver code
using System ; class GFG { static String generateString ( int N , int M , int K ) { String s = " " ; int cnt1 = 0 ; int cnt2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cnt1 ++ ; cnt2 ++ ; if ( cnt1 <= M ) { if ( cnt2 <= K ) { s = s + ( char ) ( 96 + cnt1 ) ; } else { s = s + ' a ' ; } } else { cnt1 = 1 ; cnt2 = 1 ; s = s + ' a ' ; } } return s ; } public static void Main ( String [ ] args ) { int N = 7 , M = 5 , K = 3 ; Console . WriteLine ( generateString ( N , M , K ) ) ; } }
Print string after removing all ( “ 10 ” or “ 01 ” ) from the binary string | C # program to print the readonly String after removing all the occurrences of "10" and "01" from the given binary String ; Function to print the readonly String after removing all the occurrences of "10" and "01" from the given binary String ; Variables to store the count of 1 ' s ▁ and ▁ 0' s ; Variable left will store whether 0 ' s ▁ or ▁ 1' s is left in the readonly String ; Length of the String ; For loop to count the occurrences of 1 ' s ▁ and ▁ 0' s in the String ; To check if the count of 1 ' s ▁ is ▁ ▁ greater ▁ than ▁ the ▁ count ▁ of ▁ 0' s or not . If x is greater , then those many 1 's  are printed. ; Length of the readonly remaining String after removing all the occurrences ; Printing the readonly String ; Driver Code
using System ; class GFG { static void finalString ( String str ) { int x = 0 , y = 0 ; int left ; int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) x ++ ; else y ++ ; } if ( x > y ) left = 1 ; else left = 0 ; int length = n - 2 * Math . Min ( x , y ) ; for ( int i = 0 ; i < length ; i ++ ) { Console . Write ( left ) ; } } public static void Main ( String [ ] args ) { String str = "010110100100000" ; finalString ( str ) ; } }
Longest palindrome formed by concatenating and reordering strings of equal length | C # program to find the longest palindrome that can be formed by concatenating and reordering given N Strings of equal length ; Function to print the longest palindrome ; Printing every String in left vector ; Printing the palindromic String in the middle ; Printing the reverse of the right vector to make the readonly output palindromic ; Function to find and print the longest palindrome that can be formed ; Inserting each String in the set ; Lists to add the Strings in the left and right side ; To add the already present palindrome String in the middle of the solution ; Iterating through all the given Strings ; If the String is a palindrome it is added in the middle ; Checking if the reverse of the String is already present in the set ;  ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printPalindrome ( List < String > left , String mid , List < String > right ) { foreach ( String x in left ) Console . Write ( x ) ; Console . Write ( mid ) ; right . Reverse ( ) ; foreach ( String x in right ) Console . Write ( x ) ; Console . WriteLine ( ) ; } static void findPalindrome ( List < String > S , int N , int M ) { HashSet < String > dict = new HashSet < String > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { dict . Add ( S [ i ] ) ; } List < String > left = new List < String > ( ) , right = new List < String > ( ) ; String mid = " " ; for ( int i = 0 ; i < N ; i ++ ) { String t = S [ i ] ; t = reverse ( t ) ; if ( t == S [ i ] ) mid = t ; else if ( dict . Contains ( t ) ) { left . Add ( S [ i ] ) ; right . Add ( t ) ; dict . Remove ( S [ i ] ) ; dict . Remove ( t ) ; } } printPalindrome ( left , mid , right ) ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { " tab " , " one " , " bat " } ; List < String > S = new List < String > ( arr ) ; int M = 3 ; int N = S . Count ; findPalindrome ( S , N , M ) ; } }
Lexicographically smaller string by swapping at most one character pair | C # program check whether is it possible to make String A lexicographically smaller than String B ; Swap function ; Function that finds whether is it possible to make String A lexicographically smaller than String B ; Condition if String A is already smaller than B ; Sorting temp String ; Condition for first changed character of String A and temp ; Condition if String A is already sorted ; Finding first changed character from last of String A ; Swap the two characters ; Condition if String A is smaller than B ; Driver Code
using System ; class GFG { static string swap ( string str , int i , int j ) { char [ ] tempArr = str . ToCharArray ( ) ; char temp = tempArr [ i ] ; tempArr [ i ] = tempArr [ j ] ; tempArr [ j ] = temp ; return new string ( tempArr ) ; } static bool IsLexicographicallySmaller ( string A , string B ) { if ( A . CompareTo ( B ) < 0 ) { return true ; } string temp = A ; char [ ] p = temp . ToCharArray ( ) ; Array . Sort ( p ) ; temp = new string ( p ) ; int index = - 1 ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( A [ i ] != temp [ i ] ) { index = i ; break ; } } if ( index == - 1 ) { return false ; } int j = 0 ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( A [ i ] == temp [ index ] ) j = i ; } A = swap ( A , index , j ) ; if ( A . CompareTo ( B ) < 0 ) { return true ; } else { return false ; } } public static void Main ( string [ ] args ) { string A = " AGAIN " ; string B = " ACTION " ; if ( IsLexicographicallySmaller ( A , B ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Length of longest palindromic sub | C # implementation to find the length of longest palindromic sub - String using Recursion ; Function to find maximum of the two variables ; Function to find the longest palindromic subString : Recursion ; Base condition when the start index is greater than end index ; Base condition when both the start and end index are equal ; Condition when corner characters are equal in the String ; Recursive call to find the longest Palindromic String by excluding the corner characters ; Recursive call to find the longest Palindromic String by including one corner character at a time ; Function to find the longest palindromic sub - String ; Utility function call ; Driver Code ; Function Call
using System ; class GFG { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static int longestPalindromic ( String str , int i , int j , int count ) { if ( i > j ) return count ; if ( i == j ) return ( count + 1 ) ; if ( str [ i ] == str [ j ] ) { count = longestPalindromic ( str , i + 1 , j - 1 , count + 2 ) ; return max ( count , max ( longestPalindromic ( str , i + 1 , j , 0 ) , longestPalindromic ( str , i , j - 1 , 0 ) ) ) ; } return Math . Max ( longestPalindromic ( str , i + 1 , j , 0 ) , longestPalindromic ( str , i , j - 1 , 0 ) ) ; } static int longest_palindromic_substr ( String str ) { return longestPalindromic ( str , 0 , str . Length - 1 , 0 ) ; } public static void Main ( String [ ] args ) { String str = " aaaabbaa " ; Console . Write ( longest_palindromic_substr ( str ) ) ; } }
Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency | C # implementation to find the prefix of the s such that occurrence of each character is atmost the count of minimum frequency in the s ; Function to find the maximum possible prefix of the s ; Hash map to store the frequency of the characters in the s ; Iterate over the s to find the occurence of each Character ; Minimum frequency of the Characters ; Loop to find the count of minimum frequency in the hash - map ; Loop to find the maximum possible length of the prefix in the s ; Condition to check if the frequency is greater than minimum possible freq ; maxprefix s and its length . ; Driver Code ; s is initialize . ; str is passed in MaxPrefix function .
using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static void MaxPrefix ( string s ) { Dictionary < char , int > Dict = new Dictionary < char , int > ( ) ; foreach ( char i in s ) { if ( Dict . ContainsKey ( i ) ) { Dict [ i ] ++ ; } else { Dict [ i ] = 1 ; } } int minfrequency = Int32 . MaxValue ; foreach ( int x in Dict . Values . ToList ( ) ) { minfrequency = Math . Min ( minfrequency , x ) ; } int countminFrequency = 0 ; foreach ( char x in Dict . Keys . ToList ( ) ) { if ( Dict [ x ] == minfrequency ) countminFrequency += 1 ; } Dictionary < char , int > mapper = new Dictionary < char , int > ( ) ; int indi = 0 ; foreach ( char i in s ) { if ( mapper . ContainsKey ( i ) ) { mapper [ i ] ++ ; } else { mapper [ i ] = 1 ; } if ( mapper [ i ] > countminFrequency ) break ; indi += 1 ; } Console . Write ( s . Substring ( 0 , indi ) ) ; } public static void Main ( string [ ] args ) { string str = " aabcdaab " ; MaxPrefix ( str ) ; } }
Count of Substrings that can be formed without using the given list of Characters | C # implementation of the above approach ; Function to find the Number of sub - Strings without using given character ; Mark the given characters in the freq array ; Count variable to store the count of the characters until a character from given L is encountered ; If a character from L is encountered , then the answer variable is incremented by the value obtained by using the mentioned formula and count is set to 0 ; For last remaining characters ; Driver code
using System ; class GFG { static int countSubString ( char [ ] S , char [ ] L , int n ) { int [ ] freq = new int [ 26 ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ ( int ) ( L [ i ] - ' a ' ) ] = 1 ; } int count = 0 ; foreach ( int x in S ) { if ( freq [ ( int ) ( x - ' a ' ) ] > 0 ) { ans += ( count * count + count ) / 2 ; count = 0 ; } else count ++ ; } ans += ( count * count + count ) / 2 ; return ans ; } public static void Main ( ) { string S = " abcpxyz " ; char [ ] L = { ' a ' , ' p ' , ' q ' } ; int n = L . Length ; Console . WriteLine ( countSubString ( S . ToCharArray ( ) , L , n ) ) ; } }
Program to accept Strings starting with a Vowel | C # program to accept String starting with Vowel ; Function to check if first character is vowel ; Function to check ; Driver code
using System ; class GFG { static int checkIfStartsWithVowels ( char [ ] str ) { if ( ! ( str [ 0 ] == ' A ' str [ 0 ] == ' a ' str [ 0 ] == ' E ' str [ 0 ] == ' e ' str [ 0 ] == ' I ' str [ 0 ] == ' i ' str [ 0 ] == ' O ' str [ 0 ] == ' o ' str [ 0 ] == ' U ' str [ 0 ] == ' u ' ) ) return 1 ; else return 0 ; } static void check ( String str ) { if ( checkIfStartsWithVowels ( str . ToCharArray ( ) ) == 1 ) Console . Write ( " Not ▁ Accepted STRNEWLINE " ) ; else Console . Write ( " Accepted STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String str = " animal " ; check ( str ) ; str = " zebra " ; check ( str ) ; } }
Find the Nth occurrence of a character in the given String | C # implementation to find the Nth occurrence of a character ; Function to find the Nth occurrence of a character ; Loop to find the Nth occurrence of the character ; Driver Code
using System ; class GFG { static int findNthOccur ( String str , char ch , int N ) { int occur = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ch ) { occur += 1 ; } if ( occur == N ) return i ; } return - 1 ; } public static void Main ( String [ ] args ) { String str = " geeks " ; char ch = ' e ' ; int N = 2 ; Console . Write ( findNthOccur ( str , ch , N ) ) ; } }
Longest equal substring with cost less than K | C # program to find the maximum length of equal subString within a given cost ; Function to find the maximum length ; Fill the prefix array with the difference of letters ; Update the maximum length ; Driver code
using System ; class GFG { static int solve ( string X , string Y , int N , int K ) { int [ ] count = new int [ N + 1 ] ; int sol = 0 ; count [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count [ i ] = count [ i - 1 ] + Math . Abs ( X [ i - 1 ] - Y [ i - 1 ] ) ; } int j = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { while ( ( count [ i ] - count [ j ] ) > K ) { j ++ ; } sol = Math . Max ( sol , i - j ) ; } return sol ; } public static void Main ( ) { int N = 4 ; string X = " abcd " , Y = " bcde " ; int K = 3 ; Console . WriteLine ( solve ( X , Y , N , K ) + " STRNEWLINE " ) ; } }
Jaro and Jaro | C # implementation of above approach ; Function to calculate the Jaro Similarity of two strings ; If the strings are equal ; Length of two strings ; Maximum distance upto which matching is allowed ; Count of matches ; Hash for matches ; Traverse through the first string ; Check if there is any matches ; If there is a match ; If there is no match ; Number of transpositions ; Count number of occurrences where two characters match but there is a third matched character in between the indices ; Find the next matched character in second string ; Return the Jaro Similarity ; Jaro Winkler Similarity ; If the jaro Similarity is above a threshold ; Find the length of common prefix ; If the characters match ; Else break ; Maximum of 4 characters are allowed in prefix ; Calculate jaro winkler Similarity ; Driver code ; Print Jaro - Winkler Similarity of two strings
using System ; class GFG { static double jaro_distance ( string s1 , string s2 ) { if ( s1 == s2 ) return 1.0 ; int len1 = s1 . Length , len2 = s2 . Length ; if ( len1 == 0 len2 == 0 ) return 0.0 ; int max_dist = ( int ) Math . Floor ( ( double ) Math . Max ( len1 , len2 ) / 2 ) - 1 ; int match = 0 ; int [ ] hash_s1 = new int [ s1 . Length ] ; int [ ] hash_s2 = new int [ s2 . Length ] ; for ( int i = 0 ; i < len1 ; i ++ ) { for ( int j = Math . Max ( 0 , i - max_dist ) ; j < Math . Min ( len2 , i + max_dist + 1 ) ; j ++ ) if ( s1 [ i ] == s2 [ j ] && hash_s2 [ j ] == 0 ) { hash_s1 [ i ] = 1 ; hash_s2 [ j ] = 1 ; match ++ ; break ; } } if ( match == 0 ) return 0.0 ; double t = 0 ; int point = 0 ; for ( int i = 0 ; i < len1 ; i ++ ) if ( hash_s1 [ i ] == 1 ) { while ( hash_s2 [ point ] == 0 ) point ++ ; if ( s1 [ i ] != s2 [ point ++ ] ) t ++ ; } t /= 2 ; return ( ( ( double ) match ) / ( ( double ) len1 ) + ( ( double ) match ) / ( ( double ) len2 ) + ( ( double ) match - t ) / ( ( double ) match ) ) / 3.0 ; } static double jaro_Winkler ( string s1 , string s2 ) { double jaro_dist = jaro_distance ( s1 , s2 ) ; if ( jaro_dist > 0.7 ) { int prefix = 0 ; for ( int i = 0 ; i < Math . Min ( s1 . Length , s2 . Length ) ; i ++ ) { if ( s1 [ i ] == s2 [ i ] ) prefix ++ ; else break ; } prefix = Math . Min ( 4 , prefix ) ; jaro_dist += 0.1 * prefix * ( 1 - jaro_dist ) ; } return jaro_dist ; } public static void Main ( ) { string s1 = " TRATE " , s2 = " TRACE " ; Console . WriteLine ( " Jaro - Winkler ▁ Similarity ▁ = " + jaro_Winkler ( s1 , s2 ) ) ; } }
Check if a word is present in a sentence | C # implementation of the approach ; Function that returns true if the word is found ; To break the sentence in words ; To temporarily store each individual word ; Comparing the current word with the word to be searched ; Driver code
using System ; class GFG { static bool isWordPresent ( String sentence , String word ) { String [ ] s = sentence . Split ( ' ▁ ' ) ; foreach ( String temp in s ) { if ( temp . CompareTo ( word ) == 0 ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { String s = " Geeks ▁ for ▁ Geeks " ; String word = " Geeks " ; if ( isWordPresent ( s , word ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if a word is present in a sentence | C # implementation of the approach ; Function that returns true if the word is found ; To convert the word in uppercase ; To convert the complete sentence in uppercase ; To break the sentence in words ; To store the individual words of the sentence ; Comparing the current word with the word to be searched ; Driver code
using System ; class GFG { static bool isWordPresent ( String sentence , String word ) { word = transform ( word ) ; sentence = transform ( sentence ) ; String [ ] s = sentence . Split ( ' ▁ ' ) ; foreach ( String temp in s ) { if ( temp . CompareTo ( word ) == 0 ) { return true ; } } return false ; } static String transform ( String word ) { return word . ToUpper ( ) ; } public static void Main ( String [ ] args ) { String s = " Geeks ▁ for ▁ Geeks " ; String word = " geeks " ; if ( isWordPresent ( s , word ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Count of 1 | C # implementation of the above approach ; Function to return the count of required characters ; While there are characters left ; Single bit character ; Two - bit character ; Update the count ; Driver code
using System ; class GFG { static int countChars ( string str , int n ) { int i = 0 , cnt = 0 ; while ( i < n ) { if ( str [ i ] == '0' ) i += 1 ; else i += 2 ; cnt += 1 ; } return cnt ; } public static void Main ( ) { string str = "11010" ; int n = str . Length ; Console . WriteLine ( countChars ( str , n ) ) ; } }
Print the frequency of each character in Alphabetical order | C # implementation of the approach ; Function to print the frequency of each of the characters of s in alphabetical order ; To store the frequency of the characters ; Update the frequency array ; Print the frequency in alphatecial order ; If the current alphabet doesn 't  appear in the string ; Driver code
using System ; class GFG { static int MAX = 26 ; static void compressString ( string s , int n ) { int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] == 0 ) continue ; Console . Write ( ( char ) ( i + ' a ' ) + " " + freq [ i ] ) ; } } public static void Main ( ) { string s = " geeksforgeeks " ; int n = s . Length ; compressString ( s , n ) ; } }
Find the number obtained after concatenation of binary representation of M and N | C # implementation of the approach ; Function to convert decimal number n to its binary representation stored as an array [ ] arr ; Funtion to convert the number represented as a binary array [ ] arr into its decimal equivalent ; Function to concatenate the binary numbers and return the decimal result ; Number of bits in both the numbers ; Convert the bits in both the integers to the arrays [ ] a and [ ] b ; c [ ] will be the binary array for the result ; Update the c [ ] array ; Return the decimal equivalent of the result ; Driver code
using System ; class GFG { static void decBinary ( int [ ] arr , int n ) { int k = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; while ( n > 0 ) { arr [ k -- ] = n % 2 ; n /= 2 ; } } static int binaryDec ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += arr [ i ] << ( n - i - 1 ) ; return ans ; } static int concat ( int m , int n ) { int k = ( int ) ( Math . Log ( m ) / Math . Log ( 2 ) ) + 1 ; int l = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; int [ ] a = new int [ k ] ; int [ ] b = new int [ l ] ; int [ ] c = new int [ k + l ] ; decBinary ( a , m ) ; decBinary ( b , n ) ; int iN = 0 ; for ( int i = 0 ; i < k ; i ++ ) c [ iN ++ ] = a [ i ] ; for ( int i = 0 ; i < l ; i ++ ) c [ iN ++ ] = b [ i ] ; return ( binaryDec ( c , k + l ) ) ; } public static void Main ( String [ ] args ) { int m = 4 , n = 5 ; Console . WriteLine ( concat ( m , n ) ) ; } }
Find the number obtained after concatenation of binary representation of M and N | C # implementation of the approach ; Utility function to calculate binary length of a number . ; Function to concatenate the binary numbers and return the decimal result ; Find binary length of n ; left binary shift m and then add n ; Driver code
using System ; class GFG { static int getBinaryLength ( int n ) { int length = 0 ; while ( n > 0 ) { length += 1 ; n /= 2 ; } return length ; } static int concat ( int m , int n ) { int length = getBinaryLength ( n ) ; return ( m << length ) + n ; } static void Main ( ) { int m = 4 , n = 5 ; Console . WriteLine ( concat ( m , n ) ) ; } }
XOR two binary strings of unequal lengths | C # implementation of the approach ; Function to insert n 0 s in the beginning of the given string ; Function to return the XOR of the given strings ; Lengths of the given strings ; Make both the strings of equal lengths by inserting 0 s in the beginning ; Updated length ; To store the resultant XOR ; Driver code
using System ; class GFG { static String addZeros ( String str , int n ) { for ( int i = 0 ; i < n ; i ++ ) { str = "0" + str ; } return str ; } static String getXOR ( String a , String b ) { int aLen = a . Length ; int bLen = b . Length ; if ( aLen > bLen ) { a = addZeros ( b , aLen - bLen ) ; } else if ( bLen > aLen ) { a = addZeros ( a , bLen - aLen ) ; } int len = Math . Max ( aLen , bLen ) ; String res = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( a [ i ] == b [ i ] ) res += "0" ; else res += "1" ; } return res ; } public static void Main ( String [ ] args ) { String a = "11001" , b = "111111" ; Console . WriteLine ( getXOR ( a , b ) ) ; } }
Generate Binary Strings of length N using Branch and Bound | C # Program to generate Binary Strings using Branch and Bound ; Creating a Node class ; Queue that maintains the list of live Nodes ; Utility function to generate binary strings of length n ; If list is full print combination ; Create a new vector for new combination ; iterate while length is not equal to n ; Driver code ; Initiate Generation Create a root Node ; Instantiate the Queue
using System ; using System . Collections . Generic ; public class Node { public int [ ] soln ; public int level ; public List < Node > child ; public Node parent ; public Node ( Node parent , int level , int N ) { this . parent = parent ; this . level = level ; this . soln = new int [ N ] ; } } class GFG { static int N ; public static Queue < Node > Q ; public static void generate ( Node n ) { if ( n . level == N ) { for ( int i = 0 ; i <= N - 1 ; i ++ ) { Console . Write ( n . soln [ i ] ) ; } Console . WriteLine ( ) ; } else { n . child = new List < Node > ( ) ; int l = n . level ; for ( int i = 0 ; i <= 1 ; i ++ ) { Node x = new Node ( n , l + 1 , N ) ; for ( int k = 0 ; k < l ; k ++ ) { x . soln [ k ] = n . soln [ k ] ; } x . soln [ l ] = i ; n . child . Add ( x ) ; Q . Enqueue ( x ) ; } } } public static void Main ( String [ ] args ) { N = 3 ; Node root = new Node ( null , 0 , N ) ; Q = new Queue < Node > ( ) ; Q . Enqueue ( root ) ; while ( Q . Count != 0 ) { Node E = Q . Dequeue ( ) ; generate ( E ) ; } } }