codeparrot/xlcost-text-to-code huggingface dataset:
Round the given number to nearest multiple of 10 | Set | C # implementation of the approach ; Function to round the given number to the nearest multiple of 10 ; If string is empty ; If the last digit is less then or equal to 5 then it can be rounded to the nearest ( previous ) multiple of 10 by just replacing the last digit with 0 ; Set the last digit to 0 ; Print the updated number ; The number hast to be rounded to the next multiple of 10 ; To store the carry ; Replace the last digit with 0 ; Starting from the second last digit , add 1 to digits while there is carry ; While there are digits to consider and there is carry to add ; Get the current digit ; Add the carry ; If the digit exceeds 9 then the carry will be generated ; Else there will be no carry ; Update the current digit ; Get to the previous digit ; If the carry is still 1 then it must be inserted at the beginning of the string ; Prin the rest of the number ; Driver code
using System ; using System . Text ; class GFG { static void roundToNearest ( StringBuilder str , int n ) { if ( str . ToString ( ) == " " ) return ; if ( str [ n - 1 ] - '0' <= 5 ) { str [ n - 1 ] = '0' ; Console . Write ( str . ToString ( ) . Substring ( 0 , n ) ) ; } else { int carry = 0 ; str [ n - 1 ] = '0' ; int i = n - 2 ; carry = 1 ; while ( i >= 0 && carry == 1 ) { int currentDigit = str [ i ] - '0' ; currentDigit += carry ; if ( currentDigit > 9 ) { carry = 1 ; currentDigit = 0 ; } else carry = 0 ; str [ i ] = ( char ) ( currentDigit + '0' ) ; i -- ; } if ( carry == 1 ) Console . Write ( carry ) ; Console . Write ( str . ToString ( ) . Substring ( 0 , n ) ) ; } } public static void Main ( String [ ] args ) { StringBuilder str = new StringBuilder ( "99999999999999993" ) ; int n = str . Length ; roundToNearest ( str , n ) ; } }
Repeat substrings of the given String required number of times | C # implementation of the approach ; Function that returns true if the passed character is a digit ; Function to return the next index of a non - digit character in the string starting at the index i ( returns - 1 ifno such index is found ) ; If the character at index i is a digit then skip to the next character ; If no such index was found ; Function to append str the given number of times to the StringBuilder ; Function to return the string after performing the given operations ; To build the resultant string ; Index of the first non - digit character in the string ; While there are substrings that do not consist of digits ; Find the ending of the substring ; Starting index of the number ; If no digit appears after the current substring ; Find the index at which the current number ends ; Parse the number from the substring ; Repeat the current substring required number of times ; Find the next non - digit character index ; Return the resultant string ; Driver code
using System ; using System . Text ; class GFG { static bool isDigit ( char ch ) { if ( ch >= '0' && ch <= '9' ) return true ; return false ; } static int nextNonDigit ( string str , int i ) { while ( i < str . Length && isDigit ( str [ i ] ) ) { i ++ ; } if ( i >= str . Length ) return - 1 ; return i ; } static void appendRepeated ( StringBuilder sb , string str , int times ) { for ( int i = 0 ; i < times ; i ++ ) sb . Append ( str ) ; } static String findString ( string str , int n ) { StringBuilder sb = new StringBuilder ( " " ) ; int startStr = nextNonDigit ( str , 0 ) ; while ( startStr != - 1 ) { int endStr = startStr ; while ( ( endStr + 1 ) < n && ! isDigit ( str [ endStr + 1 ] ) ) { endStr ++ ; } int startNum = endStr + 1 ; if ( startNum == - 1 ) break ; int endNum = startNum ; while ( ( endNum + 1 ) < n && isDigit ( str [ endNum + 1 ] ) ) { endNum ++ ; } int num = Int32 . Parse ( str . Substring ( startNum , endNum - startNum + 1 ) ) ; appendRepeated ( sb , str . Substring ( startStr , endStr - startStr + 1 ) , num ) ; startStr = nextNonDigit ( str , endStr + 1 ) ; } return sb . ToString ( ) ; } public static void Main ( string [ ] args ) { string str = " g1ee1ks1for1g1e2ks1" ; int n = str . Length ; Console . Write ( findString ( str , n ) ) ; } }
Recursive program to replace all occurrences of pi with 3.14 in a given string | C # program for above approach ; A simple recursive approach to replace all pi in a given function with "3.14" . Firstly function is declared we don 't  need any helper function one  function is enough ; Base case if s is empty or length of s is 1 return the s ; If the 0 th and 1 st element of s are p and i we have to handle them for rest we have to call recursion it will give the result ; Smalloutput is a variable used to store recursion result ; And we have to add the recursion result with the first part we handled and return the answer ; If not then we have to call recursion from 1 st to all elements then add recursion result to 1 st element and return it ; Driver Code ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static string replacePi ( string s ) { if ( s . Length == 0 s . Length == 1 ) return s ; if ( s [ 0 ] == ' p ' && s [ 1 ] == ' i ' ) { string smallOutput = replacePi ( s . Substring ( 2 ) ) ; return "3.14" + smallOutput ; } else { string smallOutput = replacePi ( s . Substring ( 1 ) ) ; return s [ 0 ] + smallOutput ; } } public static void Main ( string [ ] args ) { string s = " pipppiiipi " ; string result = replacePi ( s ) ; Console . Write ( result ) ; } }
Check if the given string is vowel prime | C # implementation of the approach ; Function that returns true if c is a vowel ; Function that returns true if all the vowels in the given string are only at prime indices ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; For every character of the given string ; If current character is vowel and the index is not prime ; Driver code
using System ; class GFG { static Boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static Boolean isVowelPrime ( String str , int n ) { Boolean [ ] prime = new Boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < n ; i += p ) prime [ i ] = false ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( isVowel ( str [ i ] ) && ! prime [ i ] ) return false ; } return true ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . Length ; if ( isVowelPrime ( str , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Swap all occurrences of two characters to get lexicographically smallest string | C # implementation of the approach ; Function to return the lexicographically smallest string after swapping all the occurrences of any two characters ; To store the first index of every character of str ; Store the first occurring index every character ; If current character is appearing for the first time in str ; Starting from the leftmost character ; For every character smaller than str [ i ] ; If there is a character in str which is smaller than str [ i ] and appears after it ; If the required character pair is found ; If swapping is possible ; Characters to be swapped ; For every character ; Replace every ch1 with ch2 and every ch2 with ch1 ; Driver code
using System ; class GFG { static int MAX = 26 ; static String smallestStr ( char [ ] str , int n ) { int i , j = 0 ; int [ ] chk = new int [ MAX ] ; for ( i = 0 ; i < MAX ; i ++ ) chk [ i ] = - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( chk [ str [ i ] - ' a ' ] == - 1 ) chk [ str [ i ] - ' a ' ] = i ; } for ( i = 0 ; i < n ; i ++ ) { Boolean flag = false ; for ( j = 0 ; j < str [ i ] - ' a ' ; j ++ ) { if ( chk [ j ] > chk [ str [ i ] - ' a ' ] ) { flag = true ; break ; } } if ( flag ) break ; } if ( i < n ) { char ch1 = str [ i ] ; char ch2 = ( char ) ( j + ' a ' ) ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ch1 ) str [ i ] = ch2 ; else if ( str [ i ] == ch2 ) str [ i ] = ch1 ; } } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " ccad " ; int n = str . Length ; Console . WriteLine ( smallestStr ( str . ToCharArray ( ) , n ) ) ; } }
Longest sub string of 0 's in a binary string which is repeated K times | C # program to find the Longest continuous sequence of '0' after repeating Given string K time ; Function to find the longest substring of 0 's ; To store size of the string ; To store the required answer ; Find the longest substring of 0 's ; Run a loop upto s [ i ] is zero ; Check the conditions ; Driver code ; Function call
using System ; class GFG { static int longest_substring ( String s , int k ) { int n = s . Length ; if ( k > 1 ) { s += s ; n *= 2 ; } int ans = 0 ; int i = 0 ; while ( i < n ) { int x = 0 ; while ( i < n && s [ i ] == '0' ) { x ++ ; i ++ ; } ans = Math . Max ( ans , x ) ; i ++ ; } if ( k == 1 ans != n ) return ans ; else return ( ans / 2 ) * k ; } public static void Main ( String [ ] args ) { String s = "010001000" ; int k = 4 ; Console . WriteLine ( longest_substring ( s , k ) ) ; } }
Find the number of occurrences of a character upto preceding position | C # program to find the number of occurrences of a character at position P upto p - 1 ; Function to find the number of occurrences of a character at position P upto p - 1 ; Return the required count ; Driver code ; Function call
using System ; class GFG { static int Occurrence ( String s , int position ) { int count = 0 ; for ( int i = 0 ; i < position - 1 ; i ++ ) if ( s [ i ] == s [ position - 1 ] ) count ++ ; return count ; } public static void Main ( String [ ] args ) { String s = " ababababab " ; int p = 9 ; Console . WriteLine ( Occurrence ( s , p ) ) ; } }
Find the number of occurrences of a character upto preceding position | C # program to find the number of occurrences of a character at position P upto p - 1 ; Function to find the number of occurrences of a character at position P upto p - 1 ; Iterate over the string ; Store the Occurrence of same character ; Increase its frequency ; Return the required count ; Driver code ; Function call
using System ; class GFG { static int countOccurrence ( String s , int position ) { int [ ] alpha = new int [ 26 ] ; int [ ] b = new int [ s . Length ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { b [ i ] = alpha [ ( int ) s [ i ] - 97 ] ; alpha [ ( int ) s [ i ] - 97 ] ++ ; } return b [ position - 1 ] ; } public static void Main ( String [ ] args ) { String s = " ababababab " ; int p = 9 ; Console . WriteLine ( countOccurrence ( s , p ) ) ; } }
Map every character of one string to another such that all occurrences are mapped to the same character | C # implementation of the approach ; Function that returns true if the mapping is possible ; Both the strings are of un - equal lengths ; To store the frequencies of the characters in both the string ; Update frequencies of the characters ; For every character of s1 ; If current character is not present in s1 ; Find a character in s2 that has frequency equal to the current character 's  frequency in s1 ; If such character is found ; Set the frequency to - 1 so that it doesn 't get picked again ; Set found to true ; If there is no character in s2 that could be mapped to the current character in s1 ; Driver code
using System ; class GFG { static int MAX = 26 ; public static Boolean canBeMapped ( String s1 , int l1 , String s2 , int l2 ) { if ( l1 != l2 ) return false ; int [ ] freq1 = new int [ MAX ] ; int [ ] freq2 = new int [ MAX ] ; for ( int i = 0 ; i < l1 ; i ++ ) freq1 [ s1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < l2 ; i ++ ) freq2 [ s2 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq1 [ i ] == 0 ) continue ; Boolean found = false ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq1 [ i ] == freq2 [ j ] ) { freq2 [ j ] = - 1 ; found = true ; break ; } } if ( ! found ) return false ; } return true ; } public static void Main ( String [ ] args ) { String s1 = " axx " ; String s2 = " cbc " ; int l1 = s1 . Length ; int l2 = s2 . Length ; if ( canBeMapped ( s1 , l1 , s2 , l2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Distinct strings such that they contains given strings as sub | C # implementation of the approach ; Set to store strings and avoid duplicates ; Recursive function to generate the required strings ; If current string is part of the result ; Insert it into the set ; If character from str1 can be chosen ; If character from str2 can be chosen ; Function to print the generated strings from the set ; Driver code
using System ; using System . Collections . Generic ; class GFG { static HashSet < String > stringSet = new HashSet < String > ( ) ; public static void find_permutation ( String str1 , String str2 , int len1 , int len2 , int i , int j , String res ) { if ( res . Length == len1 + len2 ) { stringSet . Add ( res ) ; return ; } if ( i < len1 ) find_permutation ( str1 , str2 , len1 , len2 , i + 1 , j , res + str1 [ i ] ) ; if ( j < len2 ) find_permutation ( str1 , str2 , len1 , len2 , i , j + 1 , res + str2 [ j ] ) ; } public static void print_set ( ) { foreach ( String s in stringSet ) Console . WriteLine ( s ) ; } public static void Main ( String [ ] args ) { String str1 = " aa " , str2 = " ab " ; int len1 = str1 . Length ; int len2 = str2 . Length ; find_permutation ( str1 , str2 , len1 , len2 , 0 , 0 , " " ) ; print_set ( ) ; } }
Generate all possible strings such that char at index i is either str1 [ i ] or str2 [ i ] | C # implementation of the approach ; Recursive function to generate the required strings ; If length of the current string is equal to the length of the given strings then the current string is part of the result ; Choosing the current character from the string a ; Choosing the current character from the string b ; Driver code ; Third argument is an empty string that we will be appended in the recursion calls Fourth arguments is the length of the resultant string so far
using System ; class GFG { public static void generateStr ( String a , String b , String s , int count , int len ) { if ( count == len ) { Console . WriteLine ( s ) ; return ; } generateStr ( a . Substring ( 1 ) , b . Substring ( 1 ) , s + a [ 0 ] , count + 1 , len ) ; generateStr ( a . Substring ( 1 ) , b . Substring ( 1 ) , s + b [ 0 ] , count + 1 , len ) ; } public static void Main ( String [ ] args ) { String a = " abc " , b = " def " ; int n = a . Length ; generateStr ( a , b , " " , 0 , n ) ; } }
Count of non | C # implementation of the approach ; Function to return the count of required non - overlapping sub - strings ; To store the required count ; If "010" matches the sub - string starting at current index i ; If "101" matches the sub - string starting at current index i ; Driver code
using System ; class GFG { static int countSubStr ( char [ ] s , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 2 ; ) { if ( s [ i ] == '0' && s [ i + 1 ] == '1' && s [ i + 2 ] == '0' ) { count ++ ; i += 3 ; } else if ( s [ i ] == '1' && s [ i + 1 ] == '0' && s [ i + 2 ] == '1' ) { count ++ ; i += 3 ; } else { i ++ ; } } return count ; } public static void Main ( String [ ] args ) { char [ ] s = "10101010101" . ToCharArray ( ) ; int n = s . Length ; Console . WriteLine ( countSubStr ( s , n ) ) ; } }
Find distinct characters in distinct substrings of a string | C # implementation of the approach ; Function to return the count of distinct characters in all the distinct sub - strings of the given string ; To store all the sub - strings ; To store the current sub - string ; To store the characters of the current sub - string ; If current sub - string hasn 't  been stored before ; Insert it into the set ; Update the count of distinct characters ; Driver code
using System ; using System . Collections . Generic ; class geeks { public static int countTotalDistinct ( String str ) { int cnt = 0 ; HashSet < String > items = new HashSet < String > ( ) ; for ( int i = 0 ; i < str . Length ; ++ i ) { String temp = " " ; HashSet < char > ans = new HashSet < char > ( ) ; for ( int j = i ; j < str . Length ; ++ j ) { temp = temp + str [ j ] ; ans . Add ( str [ j ] ) ; if ( ! items . Contains ( temp ) ) { items . Add ( temp ) ; cnt += ans . Count ; } } } return cnt ; } public static void Main ( String [ ] args ) { String str = " ABCA " ; Console . WriteLine ( countTotalDistinct ( str ) ) ; } }
Partition the string in two parts such that both parts have at least k different characters | C # implementation of the approach ; Function to find the partition of the string such that both parts have at least k different characters ; Length of the string ; To check if the current character is already found ; Count number of different characters in the left part ; If current character is not already found , increase cnt by 1 ; If count becomes equal to k , we 've  got the first part, therefore,  store current index and break the loop ; Increment i by 1 ; Clear the map ; Assign cnt as 0 ; If the current character is not already found , increase cnt by 1 ; If cnt becomes equal to k , the second part also have k different characters so break it ; If the second part has less than k different characters , then print " Not ▁ Possible " ; Otherwise print both parts ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void division_of_string ( char [ ] str , int k ) { int n = str . Length ; Dictionary < char , bool > has = new Dictionary < char , bool > ( ) ; int ans = 0 , cnt = 0 , i = 0 ; while ( i < n ) { if ( ! has . ContainsKey ( str [ i ] ) ) { cnt ++ ; has . Add ( str [ i ] , true ) ; } if ( cnt == k ) { ans = i ; break ; } i ++ ; } i ++ ; has . Clear ( ) ; cnt = 0 ; while ( i < n ) { if ( ! has . ContainsKey ( str [ i ] ) ) { cnt ++ ; has . Add ( str [ i ] , true ) ; } if ( cnt == k ) { break ; } i ++ ; } if ( cnt < k ) { Console . WriteLine ( " Not ▁ possible " ) ; } else { i = 0 ; while ( i <= ans ) { Console . Write ( str [ i ] ) ; i ++ ; } Console . WriteLine ( " " ) ; while ( i < n ) { Console . Write ( str [ i ] ) ; i ++ ; } Console . WriteLine ( " " ) ; } Console . WriteLine ( " " ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 4 ; division_of_string ( str . ToCharArray ( ) , k ) ; } }
Count substrings that contain all vowels | SET 2 | C # implementation of the approach ; Function that returns true if c is a vowel ; Function to return the count of sub - strings that contain every vowel at least once and no consonant ; Map is used to store count of each vowel ; Start index is set to 0 initially ; If substring till now have all vowels atleast once increment start index until there are all vowels present between ( start , i ) and add n - i each time ; Function to extract all maximum length sub - strings in s that contain only vowels and then calls the countSubstringsUtil ( ) to find the count of valid sub - strings in that string ; If current character is a vowel then append it to the temp string ; The sub - string containing all vowels ends here ; If there was a valid sub - string ; Reset temp string ; For the last valid sub - string ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static int countSubstringsUtil ( char [ ] s ) { int count = 0 ; Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; int n = s . Length ; int start = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( s [ i ] ) ) { mp [ s [ i ] ] = mp [ s [ i ] ] + 1 ; } else { mp . Add ( s [ i ] , 1 ) ; } while ( mp . ContainsKey ( ' a ' ) && mp . ContainsKey ( ' e ' ) && mp . ContainsKey ( ' i ' ) && mp . ContainsKey ( ' o ' ) && mp . ContainsKey ( ' u ' ) && mp [ ' a ' ] > 0 && mp [ ' e ' ] > 0 && mp [ ' i ' ] > 0 && mp [ ' o ' ] > 0 && mp [ ' u ' ] > 0 ) { count += n - i ; if ( mp . ContainsKey ( s [ start ] ) ) mp [ s [ start ] ] = mp [ s [ start ] ] - 1 ; start ++ ; } } return count ; } static int countSubstrings ( String s ) { int count = 0 ; String temp = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( isVowel ( s [ i ] ) ) { temp += s [ i ] ; } else { if ( temp . Length > 0 ) count += countSubstringsUtil ( temp . ToCharArray ( ) ) ; temp = " " ; } } if ( temp . Length > 0 ) count += countSubstringsUtil ( temp . ToCharArray ( ) ) ; return count ; } public static void Main ( String [ ] args ) { String s = " aeouisddaaeeiouua " ; Console . WriteLine ( countSubstrings ( s ) ) ; } }
Remove first adjacent pairs of similar characters until possible | C # implementation of the above approach ; Function to remove adjacent duplicates ; Iterate for every character in the string ; If ans string is empty or its last character does not match with the current character then append this character to the string ; Matches with the previous one ; Return the answer ; Driver Code
using System ; class GFG { static String removeDuplicates ( String S ) { String ans = " " ; for ( int i = 0 ; i < S . Length ; i ++ ) { if ( ans == " " ans [ ans . Length - 1 ] != S [ i ] ) ans += S [ i ] ; else if ( ans [ ans . Length - 1 ] == S [ i ] ) ans = ans . Substring ( 0 , ans . Length - 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { String str = " keexxllx " ; Console . WriteLine ( removeDuplicates ( str ) ) ; } }
Reverse individual words with O ( 1 ) extra space | C # implementation of the approach ; Function to resturn the string after reversing the individual words Function to resturn the string after reversing the individual words ; Pointer to the first character of the first word ; If the current word has ended ; Pointer to the last character of the current word ; Reverse the current word ; Pointer to the first character of the next word ; Driver code
using System ; class GFG { static String reverseWords ( String str ) { int start = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' ▁ ' i == str . Length - 1 ) { int end ; if ( i == str . Length - 1 ) end = i ; else end = i - 1 ; while ( start < end ) { str = swap ( str , start , end ) ; start ++ ; end -- ; } start = i + 1 ; } } return str ; } static String swap ( String str , int i , int j ) { char [ ] ch = str . ToCharArray ( ) ; char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . Join ( " " , ch ) ; } public static void Main ( String [ ] args ) { String str = " Geeks ▁ for ▁ Geeks " ; Console . WriteLine ( reverseWords ( str ) ) ; } }
Reverse the Words of a String using Stack | C # implementation of the approach ; Function to reverse the words of the given sentence ; Create an empty character array stack ; Push words into the stack ; Get the words in reverse order ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void reverse ( String k ) { Stack < String > s = new Stack < String > ( ) ; String [ ] token = k . Split ( ' ▁ ' ) ; for ( int i = 0 ; i < token . Length ; i ++ ) { s . Push ( token [ i ] ) ; } while ( s . Count != 0 ) { Console . Write ( s . Peek ( ) + " ▁ " ) ; s . Pop ( ) ; } } public static void Main ( String [ ] args ) { String k = " geeks ▁ for ▁ geeks " ; reverse ( k ) ; } }