codeparrot/xlcost-text-to-code huggingface dataset:
Sub | C # implementation of the approach ; Function that checks if the String contain exactly K characters having ASCII value greater than p ; if ASCII value is greater than ' p ' ; if count of satisfying characters is equal to ' K ' then return true ; otherwise return false ; function to count sub - Strings ; length of the String ; count of sub - Strings ; ' i ' is the starting index for the sub - String ; ' j ' is the no . of characters to include in the sub - String ; check if the sub - String satisfies the condition ; Driver code
using System ; class GFG { bool isValidSubString ( String r , int K , int p ) { int c = 0 ; for ( int i = 0 ; i < r . Length ; i ++ ) { if ( ( int ) r [ i ] > p ) { c ++ ; } } if ( c == K ) { return true ; } else { return false ; } } void countSubStrings ( String s , int K , int p ) { int l = s . Length ; int count = 0 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = K ; ( i + j ) <= l ; j ++ ) { String r = s . Substring ( i , j ) ; if ( isValidSubString ( r , K , p ) ) { count ++ ; } } } Console . WriteLine ( count ) ; } public static void Main ( String [ ] args ) { GFG g = new GFG ( ) ; String s = " abepztydba " ; int K = 4 ; int p = 110 ; g . countSubStrings ( s , K , p ) ; } }
Count number of substrings with numeric value greater than X | C # implementation of the approach ; Function that counts valid sub - strings ; Only take those numbers that do not start with '0' . ; converting the sub - string starting from index ' i ' and having length ' len ' to int and checking if it is greater than X or not ; Driver code
using System ; class GFG { static int count ( string S , int X ) { int count = 0 ; int N = S . Length ; for ( int i = 0 ; i < N ; ++ i ) { if ( S [ i ] != '0' ) { for ( int len = 1 ; ( i + len ) <= N ; ++ len ) { int num = Int32 . Parse ( S . Substring ( i , len ) ) ; if ( num > X ) count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { string S = "2222" ; int X = 97 ; Console . WriteLine ( count ( S , X ) ) ; } }
Check whether a binary string can be formed by concatenating given N numbers sequentially | C # implementation of the approach ; Function that returns false if the number passed as argument contains digit ( s ) other than '0' or '1' ; Function that checks whether the binary String can be formed or not ; Empty String for storing the binary number ; check if a [ i ] can be a part of the binary String ; Conversion of int into String ; if a [ i ] can 't be a part  then break the loop ; possible to create binary String ; impossible to create binary String ; Driver code
using System ; public class Solution { public static bool isBinary ( int n ) { while ( n != 0 ) { int temp = n % 10 ; if ( temp != 0 && temp != 1 ) { return false ; } n = n / 10 ; } return true ; } public static void formBinaryStr ( int n , int [ ] a ) { bool flag = true ; string s = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( isBinary ( a [ i ] ) ) { s += " " + a [ i ] ; } else { flag = false ; break ; } } if ( flag ) { Console . Write ( s + " STRNEWLINE " ) ; } else { Console . Write ( " - 1 STRNEWLINE " ) ; } } public static void Main ( string [ ] args ) { int [ ] a = new int [ ] { 10 , 1 , 0 , 11 , 10 } ; int N = a . Length ; formBinaryStr ( N , a ) ; } }
Check if all the palindromic sub | C # implementation of the approach ; Function to check if the string is palindrome ; Function that checks whether all the palindromic sub - strings are of odd length . ; Creating each substring ; If the sub - string is of even length and is a palindrome then , we return False ; Driver code
using System ; public class GFG { static bool checkPalindrome ( String s ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] != s [ ( s . Length - i - 1 ) ] ) return false ; } return true ; } static bool CheckOdd ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { String x = " " ; for ( int j = i ; j < n ; j ++ ) { x += s [ j ] ; if ( x . Length % 2 == 0 && checkPalindrome ( x ) == true ) return false ; } } return true ; } public static void Main ( ) { String s = " geeksforgeeks " ; if ( CheckOdd ( s ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Number of permutations of a string in which all the occurrences of a given character occurs together | C # implementation of above approach ; Function to return factorial of the number passed as argument ; Function to get the total permutations which satisfy the given condition ; Create has to store count of each character ; Store character occurrences ; Count number of times Particular character comes ; If particular character isn 't present in the string  then return 0 ; Remove count of particular character ; Total length of the string ; Assume all occurrences of particular character as a single character . ; Compute factorial of the length ; Divide by the factorials of the no . of occurrences of all the characters . ; return the result ; Driver Code ; Assuming the string and the character are all in uppercase
using System ; class GFG { static int fact ( int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result *= i ; return result ; } static int getResult ( string str , char ch ) { int [ ] has = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) has [ i ] = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) has [ str [ i ] - ' A ' ] ++ ; int particular = has [ ch - ' A ' ] ; if ( particular == 0 ) return 0 ; has [ ch - ' A ' ] = 0 ; int total = str . Length ; total = total - particular + 1 ; int result = fact ( total ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( has [ i ] > 1 ) { result = result / fact ( has [ i ] ) ; } } return result ; } public static void Main ( ) { string str = " MISSISSIPPI " ; Console . WriteLine ( getResult ( str , ' S ' ) ) ; } }
Check if there exists any sub | C # program to check if there exists at least 1 sub - sequence in a string which is not palindrome ; Function to check if there exists at least 1 sub - sequence in a string which is not palindrome ; use set to count number of distinct characters ; insert each character in set ; If there is more than 1 unique characters , return true ; Else , return false ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isAnyNotPalindrome ( String s ) { HashSet < char > unique = new HashSet < char > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) unique . Add ( s [ i ] ) ; if ( unique . Count > 1 ) return true ; else return false ; } public static void Main ( String [ ] args ) { String s = " aaaaab " ; if ( isAnyNotPalindrome ( s ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Arrangement of words without changing the relative position of vowel and consonants | C # program for Arrangement of words without changing the relative position of vowel and consonants ; this function return n ! ; this will return total number of ways ; freq maintains frequency of each character in word ; check character is vowel or not ; the characters that are not vowel must be consonant ; number of ways to arrange vowel ; multiply both as these are independent ; Driver function ; string contains only capital letters ; this will contain ans
using System ; class GFG { static long factorial ( long n ) { long res = 1 ; for ( int i = 1 ; i <= n ; i ++ ) res = res * i ; return res ; } static long count ( string word ) { int [ ] freq = new int [ 27 ] ; for ( int i = 0 ; i < 27 ; i ++ ) freq [ i ] = 0 ; long vowel = 0 , consonant = 0 ; for ( int i = 0 ; i < word . Length ; i ++ ) { freq [ word [ i ] - ' A ' ] ++ ; if ( word [ i ] == ' A ' word [ i ] == ' E ' word [ i ] == ' I ' word [ i ] == ' O ' word [ i ] == ' U ' ) { vowel ++ ; } else consonant ++ ; } long vowelArrange ; vowelArrange = factorial ( vowel ) ; vowelArrange /= factorial ( freq [ 0 ] ) ; vowelArrange /= factorial ( freq [ 4 ] ) ; vowelArrange /= factorial ( freq [ 8 ] ) ; vowelArrange /= factorial ( freq [ 14 ] ) ; vowelArrange /= factorial ( freq [ 20 ] ) ; long consonantArrange ; consonantArrange = factorial ( consonant ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 ) consonantArrange /= factorial ( freq [ i ] ) ; } long total = vowelArrange * consonantArrange ; return total ; } public static void Main ( ) { string word = " COMPUTER " ; long ans = count ( word ) ; Console . WriteLine ( ans ) ; } }
Check if it is possible to create a palindrome string from given N | C # implementation of the above approach ; Function to check if a string is palindrome or not ; String that stores characters of s in reverse order ; Length of the string s ; String used to form substring using N ; Variable to store sum of digits of N ; Forming the substring by traversing N ; Appending the substr to str till it 's length becomes equal to sum ; Trimming the string str so that it 's length becomes equal to sum ; Driver code ; Calling function isPalindrome to check if str is Palindrome or not
using System ; class GFG { static bool isPalindrome ( String s ) { String s1 = " " ; int N = s . Length ; for ( int i = N - 1 ; i >= 0 ; i -- ) s1 += s [ i ] ; if ( s . Equals ( s1 ) ) return true ; return false ; } static bool createString ( int N ) { String str = " " ; String s = " " + N ; String letters = " abcdefghij " ; int sum = 0 ; String substr = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { int digit = s [ i ] - '0' ; substr += letters [ digit ] ; sum += digit ; } while ( str . Length <= sum ) { str += substr ; } str = str . Substring ( 0 , sum ) ; return isPalindrome ( str ) ; } public static void Main ( ) { int N = 61 ; bool flag = createString ( N ) ; if ( flag ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Program to find the product of ASCII values of characters in a string | C # program to find product of ASCII value of characters in string ; Function to find product of ASCII value of characters in string ; Traverse string to find the product ; Return the product ; Driver Code
using System ; class GFG { static long productAscii ( String str ) { long prod = 1 ; for ( int i = 0 ; i < str . Length ; i ++ ) { prod *= str [ i ] ; } return prod ; } static public void Main ( ) { String str = " GfG " ; Console . Write ( productAscii ( str ) ) ; } }
Check if a large number can be divided into two or more segments of equal sum | C # program to Check if a large number can be divided into two or more segments of equal sum ; Function to check if a number can be divided into segments ; length of string ; array to store prefix sum ; first index ; calculate the prefix ; iterate for all number from second number ; sum from 0 th index to i - 1 th index ; counter turns true when sum is obtained from a segment ; iterate till the last number ; sum of segments ; if segment sum is equal to first segment ; when greater than not possible ; if at the end all values are traversed and all segments have sum equal to first segment then it is possible ; Driver Code
using System ; class GFG { static bool check ( String s ) { int n = s . Length ; int [ ] Presum = new int [ n ] ; char [ ] s1 = s . ToCharArray ( ) ; Presum [ 0 ] = s1 [ 0 ] - '0' ; for ( int i = 1 ; i < n ; i ++ ) { Presum [ i ] = Presum [ i - 1 ] + ( s1 [ i ] - '0' ) ; } for ( int i = 1 ; i <= n - 1 ; i ++ ) { int sum = Presum [ i - 1 ] ; int presum = 0 ; int it = i ; int flag = 0 ; while ( it < n ) { presum += s1 [ it ] - '0' ; if ( presum == sum ) { presum = 0 ; flag = 1 ; } else if ( presum > sum ) { break ; } it ++ ; } if ( presum == 0 && it == n && flag == 1 ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { String s = "73452" ; if ( check ( s ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Balance a string after removing extra brackets | C # implementation of above approach ; Print balanced and remove extra brackets from string ; Maintain a count for opening brackets Traversing string ; check if opening bracket ; print str [ i ] and increment count by 1 ; check if closing bracket and count != 0 ; decrement count by 1 ; if str [ i ] not a closing brackets print it ; balanced brackets if opening brackets are more then closing brackets ; print remaining closing brackets ; Driver Code
using System ; class GFG { public static void balancedString ( String str ) { int count = 0 , i ; int n = str . Length ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ( ' ) { Console . Write ( str [ i ] ) ; count ++ ; } else if ( str [ i ] == ' ) ' && count != 0 ) { Console . Write ( str [ i ] ) ; count -- ; } else if ( str [ i ] != ' ) ' ) Console . Write ( str [ i ] ) ; } if ( count != 0 ) for ( i = 0 ; i < count ; i ++ ) Console . Write ( " ) " ) ; } public static void Main ( ) { String str = " gau ) ra ) v ( ku ( mar ( rajput ) ) " ; balancedString ( str ) ; } }
Minimum operation require to make first and last character same | C # program to minimum operation require to make first and last character same ; Recursive function call ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Driver Code ; Function call
using System ; class GFG { static int MAX = Int32 . MaxValue ; static int minOperation ( string s , int i , int j , int count ) { if ( ( i >= s . Length && j < 0 ) || ( i == j ) ) return MAX ; if ( s [ i ] == s [ j ] ) return count ; if ( i >= s . Length ) return minOperation ( s , i , j - 1 , count + 1 ) ; else if ( j < 0 ) return minOperation ( s , i + 1 , j , count + 1 ) ; else return Math . Min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ; } public static void Main ( ) { string s = " bacdefghipalop " ; int ans = minOperation ( s , 0 , s . Length - 1 , 0 ) ; if ( ans == MAX ) Console . Write ( - 1 ) ; else Console . Write ( ans ) ; } }
Count strings with consonants and vowels at alternate position | C # implementation of above approach ; Function to find the count of strings ; Variable to store the final result ; Loop iterating through string ; If ' $ ' is present at the even position in the string ; ' sum ' is multiplied by 21 ; If ' $ ' is present at the odd position in the string ; ' sum ' is multiplied by 5 ; Driver code ; Let the string ' str ' be s$$e$ ; Print result
using System ; class GFG { static int countStrings ( String s ) { int sum = 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( i % 2 == 0 && s [ i ] == ' $ ' ) sum *= 21 ; else if ( s [ i ] == ' $ ' ) sum *= 5 ; } return sum ; } public static void Main ( ) { String str = " s $ $ e $ " ; Console . WriteLine ( countStrings ( str ) ) ; } }
Remove duplicates from a string in O ( 1 ) extra space | C # implementation of above approach ; Function to remove duplicates ; keeps track of visited characters ; gets character value ; keeps track of length of resultant String ; check if Xth bit of counter is unset ; mark current character as visited ; Driver code
using System ; class GFG { static string removeDuplicatesFromString ( string string1 ) { int counter = 0 ; char [ ] str = string1 . ToCharArray ( ) ; int i = 0 ; int size = str . Length ; int x ; int length = 0 ; while ( i < size ) { x = str [ i ] - 97 ; if ( ( counter & ( 1 << x ) ) == 0 ) { str [ length ] = ( char ) ( ' a ' + x ) ; counter = counter | ( 1 << x ) ; length ++ ; } i ++ ; } return ( new string ( str ) ) . Substring ( 0 , length ) ; } static void Main ( ) { string str = " geeksforgeeks " ; Console . WriteLine ( removeDuplicatesFromString ( str ) ) ; } }
Remove duplicates from a string in O ( 1 ) extra space | C # implementation of above approach ; Method to remove duplicates ; table to keep track of visited characters ; to keep track of end index of resultant string ; Driver code
using System ; class GFG { static char [ ] removeDuplicatesFromString ( String str ) { int [ ] table = new int [ 256 ] ; char [ ] chars = str . ToCharArray ( ) ; int endIndex = 0 ; for ( int i = 0 ; i < chars . Length ; i ++ ) { if ( table [ chars [ i ] ] == 0 ) { table [ chars [ i ] ] = - 1 ; chars [ endIndex ++ ] = chars [ i ] ; } } char [ ] newStr = new char [ endIndex ] ; Array . Copy ( chars , newStr , endIndex ) ; return newStr ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; Console . WriteLine ( removeDuplicatesFromString ( str ) ) ; } }
Check if the characters in a string form a Palindrome in O ( 1 ) extra space | C # program to check if the characters in the given string forms a Palindrome in O ( 1 ) extra space ; Utility function to get the position of first character in the string ; Get the position of first character in the string ; Utiltiy function to get the position of last character in the string ; Get the position of last character in the string ; Function to check if the characters in the given string forms a Palindrome in O ( 1 ) extra space ; break , when all letters are checked ; if mismatch found , break the loop ; Driver code
using System ; class GFG { static int firstPos ( string str , int start , int end ) { int firstChar = - 1 ; for ( int i = start ; i <= end ; i ++ ) { if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) { firstChar = i ; break ; } } return firstChar ; } static int lastPos ( string str , int start , int end ) { int lastChar = - 1 ; for ( int i = start ; i >= end ; i -- ) { if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) { lastChar = i ; break ; } } return lastChar ; } static bool isPalindrome ( string str ) { int firstChar = 0 , lastChar = str . Length - 1 ; bool ch = true ; for ( int i = 0 ; i < str . Length ; i ++ ) { firstChar = firstPos ( str , firstChar , lastChar ) ; lastChar = lastPos ( str , lastChar , firstChar ) ; if ( lastChar < 0 firstChar < 0 ) break ; if ( str [ firstChar ] == str [ lastChar ] ) { firstChar ++ ; lastChar -- ; continue ; } ch = false ; break ; } return ch ; } public static void Main ( ) { string str = " m ▁ a ▁ 343 ▁ la ▁ y ▁ a ▁ l ▁ am " ; if ( isPalindrome ( str ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Maximum length subsequence possible of the form R ^ N K ^ N | C # program to find the maximum length of a substring of form R ^ nK ^ n ; function to calculate the maximum length of substring of the form R ^ nK ^ n ; Count no . Of R 's before a K ; Count no . Of K 's after a K ; Update maximum length ; Driver code
using System ; class GFG { static int find ( String s ) { int max = 0 , i , j = 0 , countk = 0 , countr = 0 ; int [ , ] table = new int [ s . Length , 2 ] ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' R ' ) countr ++ ; else table [ ( j ++ ) , 0 ] = countr ; } j -- ; for ( i = s . Length - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' K ' ) { countk ++ ; table [ j -- , 1 ] = countk ; } if ( Math . Min ( table [ j + 1 , 0 ] , table [ j + 1 , 1 ] ) > max ) max = Math . Min ( table [ j + 1 , 0 ] , table [ j + 1 , 1 ] ) ; } return max ; } static public void Main ( String [ ] srgs ) { String s = " RKRRRKKRRKKKKRR " ; int n = find ( s ) ; Console . WriteLine ( n ) ; } }
Longest common anagram subsequence from N strings | c # program to find longest possible subsequence anagram of N strings . ; function to store frequency of each character in each string ; function to Find longest possible sequence of N strings which is anagram to each other ; to get lexicographical largest sequence . ; find minimum of that character ; print that character minimum number of times ; Driver code ; to store frequency of each character in each string ; to get frequency of each character ; function call
using System ; class GFG { public readonly int MAX_CHAR = 26 ; public static void frequency ( int [ , ] fre , string [ ] s , int n ) { for ( int i = 0 ; i < n ; i ++ ) { string str = s [ i ] ; for ( int j = 0 ; j < str . Length ; j ++ ) { fre [ i , str [ j ] - ' a ' ] ++ ; } } } public static void LongestSequence ( int [ , ] fre , int n ) { for ( int i = 24 ; i >= 0 ; i -- ) { int mi = fre [ 0 , i ] ; for ( int j = 1 ; j < n ; j ++ ) { mi = Math . Min ( fre [ j , i ] , mi ) ; } while ( mi -- != 0 ) { Console . Write ( ( char ) ( ' a ' + i ) ) ; } } } public static void Main ( string [ ] args ) { string [ ] s = new string [ ] { " loo " , " lol " , " olive " } ; int n = s . Length ; int [ , ] fre = new int [ n , 26 ] ; frequency ( fre , s , n ) ; LongestSequence ( fre , n ) ; } }
Check if two strings are permutation of each other | C # program to check whether two strings are Permutations of each other ; function to check whether two strings are Permutation of each other ; Get lengths of both strings ; If length of both strings is not same , then they cannot be Permutation ; Sort both strings ; Compare sorted strings ; Driver code
using System ; class GfG { static bool arePermutation ( String str1 , String str2 ) { int n1 = str1 . Length ; int n2 = str2 . Length ; if ( n1 != n2 ) return false ; char [ ] ch1 = str1 . ToCharArray ( ) ; char [ ] ch2 = str2 . ToCharArray ( ) ; Array . Sort ( ch1 ) ; Array . Sort ( ch2 ) ; for ( int i = 0 ; i < n1 ; i ++ ) if ( ch1 [ i ] != ch2 [ i ] ) return false ; return true ; } public static void Main ( String [ ] args ) { String str1 = " test " ; String str2 = " ttew " ; if ( arePermutation ( str1 , str2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check if two strings are permutation of each other | C # function to check whether two strings are Permutations of each other ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array
static bool arePermutation ( char [ ] str1 , char [ ] str2 ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; str1 [ i ] && str2 [ i ] ; i ++ ) { count [ str1 [ i ] ] ++ ; count [ str2 [ i ] ] -- ; } if ( str1 [ i ] str2 [ i ] ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; }
Maximum power of jump required to reach the end of string | C # program to calculate power of jump ; Function to calculate the maximum power of the jump ; Initialize the count with 1 ; Find the character at last index ; Start traversing the string ; Check if the current char is equal to the last character ; max_so_far stores maximum value of the power of the jump from starting to ith position ; Reset the count to 1 ; Else , increment the number of jumps / count ; Return the maximum number of jumps ; Driver code
using System ; class GFG { public static int powerOfJump ( String s ) { int count = 1 ; int max_so_far = int . MinValue ; char ch = s [ s . Length - 1 ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ch ) { if ( count > max_so_far ) { max_so_far = count ; } count = 1 ; } else count ++ ; } return max_so_far ; } public static void Main ( ) { String st = "1010101" ; Console . WriteLine ( powerOfJump ( st ) ) ; } }