codeparrot/xlcost-text-to-code huggingface dataset:
Print the middle character of a string | C # program for the above approach ; Function that prints the middle character of a string ; Finding string length ; Finding middle index of string ; Print the middle character of the string ; Driver Code ; Given string str ; Function call
using System ; class GFG { public static void printMiddlechar ( String str ) { int len = str . Length ; int middle = len / 2 ; Console . WriteLine ( str [ middle ] ) ; } public static void Main ( String [ ] args ) { String str = " GeeksForGeeks " ; printMiddlechar ( str ) ; } }
Maximize length of the String by concatenating characters from an Array of Strings | C # program to implement the above approach ; Function to check if all the string characters are unique ; Check for repetition in characters ; Function to generate all possible strings from the given array ; Base case ; Consider every string as a starting substring and store the generated string ; Add current string to result of other strings and check if characters are unique or not ; Function to find the maximum possible length of a string ; Return max length possible ; Return the answer ; Driver Code
using System ; using System . Collections ; using System . Collections . Generic ; using System . Text ; class GFG { static bool check ( string s ) { HashSet < char > a = new HashSet < char > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( a . Contains ( s [ i ] ) ) { return false ; } a . Add ( s [ i ] ) ; } return true ; } static ArrayList helper ( ArrayList arr , int ind ) { if ( ind == arr . Count ) return new ArrayList ( ) { " " } ; ArrayList tmp = helper ( arr , ind + 1 ) ; ArrayList ret = new ArrayList ( tmp ) ; for ( int i = 0 ; i < tmp . Count ; i ++ ) { string test = ( string ) tmp [ i ] + ( string ) arr [ ind ] ; if ( check ( test ) ) ret . Add ( test ) ; } return ret ; } static int maxLength ( ArrayList arr ) { ArrayList tmp = helper ( arr , 0 ) ; int len = 0 ; for ( int i = 0 ; i < tmp . Count ; i ++ ) { len = len > ( ( string ) tmp [ i ] ) . Length ? len : ( ( string ) tmp [ i ] ) . Length ; } return len ; } public static void Main ( string [ ] args ) { ArrayList s = new ArrayList ( ) ; s . Add ( " abcdefgh " ) ; Console . Write ( maxLength ( s ) ) ; } }
Perform range sum queries on string as per given condition | C # program for the above approach ; Function to perform range sum queries on String as per the given condition ; Initialize N by String size ; Create array [ ] A for prefix sum ; Iterate till N ; Traverse the queries ; Check if if L == 1 range sum will be A [ R - 1 ] ; Condition if L > 1 range sum will be A [ R - 1 ] - A [ L - 2 ] ; Driver Code ; Given String ; Given Queries ; Function call
using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void Range_sum_query ( String S , List < pair > Query ) { int N = S . Length ; int [ ] A = new int [ N ] ; A [ 0 ] = S [ 0 ] - ' a ' + 1 ; for ( int i = 1 ; i < N ; i ++ ) { A [ i ] = S [ i ] - ' a ' + 1 ; A [ i ] = A [ i ] + A [ i - 1 ] ; } for ( int i = 0 ; i < Query . Count ; i ++ ) { if ( Query [ i ] . first == 1 ) { Console . Write ( A [ ( Query [ i ] . second ) - 1 ] + " STRNEWLINE " ) ; } else { Console . Write ( A [ ( Query [ i ] . second ) - 1 ] - A [ ( Query [ i ] . first ) - 2 ] + " STRNEWLINE " ) ; } } } public static void Main ( String [ ] args ) { String S = " abcd " ; List < pair > Query = new List < pair > ( ) ; Query . Add ( new pair ( 2 , 4 ) ) ; Query . Add ( new pair ( 1 , 3 ) ) ; Range_sum_query ( S , Query ) ; } }
Generate all possible permutations of a Number divisible by N | C # program to implement the above approach ; Function to Swap two characters ; Function to generate all permutations and print the ones that are divisible by the N ; Convert String to integer ; Check for divisibility and print it ; Print all the permutations ; Swap characters ; Permute remaining characters ; Revoke the swaps ; Driver Code
using System ; class GFG { static void swap_ ( char [ ] a , int l , int i ) { char temp ; temp = a [ l ] ; a [ l ] = a [ i ] ; a [ i ] = temp ; } static void permute ( char [ ] str , int l , int r , int n ) { int i ; if ( l == r ) { int j = Int32 . Parse ( new string ( str ) ) ; if ( j % n == 0 ) Console . Write ( new string ( str ) + " STRNEWLINE " ) ; return ; } for ( i = l ; i < r ; i ++ ) { swap_ ( str , l , i ) ; permute ( str , l + 1 , r , n ) ; swap_ ( str , l , i ) ; } } public static void Main ( string [ ] args ) { string str = "125" ; int n = 5 ; int len = str . Length ; if ( len > 0 ) permute ( str . ToCharArray ( ) , 0 , len , n ) ; } }
Check if binary representations of 0 to N are present as substrings in given binary string | C # program for the above approach ; Function to convert decimal to binary representation ; Iterate over all bits of N ; If bit is 1 ; Return binary representation ; Function to check if binary conversion of numbers from N to 1 exists in the String as a subString or not ; To store the count of number exists as a subString ; Traverse from N to 1 ; If current number is not present in map ; Store current number ; Find binary of t ; If the String s is a subString of str ; Mark t as true ; Increment the count ; Update for t / 2 ; Special judgment '0' ; If the count is N + 1 , return " yes " ; Driver Code ; Given String ; Given number ; Function call
using System ; class GFG { static String decimalToBinary ( int N ) { String ans = " " ; while ( N > 0 ) { if ( N % 2 == 1 ) { ans = '1' + ans ; } else { ans = '0' + ans ; } N /= 2 ; } return ans ; } static String checkBinaryString ( String str , int N ) { int [ ] map = new int [ N + 10 ] ; int cnt = 0 ; for ( int i = N ; i > 0 ; i -- ) { if ( map [ i ] == 0 ) { int t = i ; String s = decimalToBinary ( t ) ; if ( str . Contains ( s ) ) { while ( t > 0 && map [ t ] == 0 ) { map [ t ] = 1 ; cnt ++ ; t >>= 1 ; } } } } for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '0' ) { cnt ++ ; break ; } } if ( cnt == N + 1 ) return " True " ; else return " False " ; } public static void Main ( String [ ] args ) { String str = "0110" ; int N = 3 ; Console . Write ( checkBinaryString ( str , N ) ) ; } }
Find the word with most anagrams in a given sentence | C # program to find the word with most anagrams in a sentence ; Function to find the word with maximum number of anagrams ; Primes assigned to 26 alphabets ; Stores the product and word mappings ; Stores the frequencies of products ; Calculate the product of primes assigned ; If product already exists ; Otherwise ; Fetch the most frequent product ; Return a string with that product ; Driver Code
using System ; using System . Collections . Generic ; class GFG { private static String largestAnagramGrp ( String [ ] arr ) { int [ ] prime = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 , 101 } ; int max = - 1 ; long maxpdt = - 1 ; Dictionary < long , String > W = new Dictionary < long , String > ( ) ; Dictionary < long , int > P = new Dictionary < long , int > ( ) ; foreach ( String temp in arr ) { char [ ] c = temp . ToCharArray ( ) ; long pdt = 1 ; foreach ( char t in c ) { pdt *= prime [ t - ' a ' ] ; } if ( P . ContainsKey ( pdt ) ) { P [ pdt ] = P [ pdt ] + 1 ; } else { W . Add ( pdt , temp ) ; P . Add ( pdt , 1 ) ; } } foreach ( KeyValuePair < long , int > e in P ) { if ( max < e . Value ) { max = e . Value ; maxpdt = e . Key ; } } return W [ maxpdt ] ; } public static void Main ( String [ ] args ) { String S = " please ▁ be ▁ silent ▁ and ▁ listen " + " ▁ to ▁ what ▁ the ▁ professor ▁ says ▁ " ; String [ ] arr = S . Split ( ' ▁ ' ) ; Console . WriteLine ( largestAnagramGrp ( arr ) ) ; } }
Replace every vowels with lexicographically next vowel in a String | C # program to convert all the vowels in in the String to the next vowel ; Function to replace every vowel with next vowel lexicographically ; Storing the vowels in the map with custom numbers showing their index ; Iterate over the String ; If the current character is a vowel Find the index in Hash and Replace it with next vowel from Hash ; Driver code
using System ; using System . Collections . Generic ; class GFG { static String print_next_vovel_String ( char [ ] str ) { Dictionary < char , int > m = new Dictionary < char , int > ( ) ; m . Add ( ' a ' , 0 ) ; m . Add ( ' e ' , 1 ) ; m . Add ( ' i ' , 2 ) ; m . Add ( ' o ' , 3 ) ; m . Add ( ' u ' , 4 ) ; char [ ] arr = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; int N = str . Length ; for ( int i = 0 ; i < N ; i ++ ) { char c = str [ i ] ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) { int index = m + 1 ; int newindex = index % 5 ; str [ i ] = arr [ newindex ] ; } } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; Console . Write ( print_next_vovel_String ( str . ToCharArray ( ) ) ) ; } }
Check if string is palindrome after removing all consecutive duplicates | C # program for the above approach ; Function to check if a String is palindrome or not ; Length of the String ; Check if its a palindrome ; If the palindromic condition is not met ; Return true as str is palindromic ; Function to check if String str is palindromic after removing every consecutive characters from the str ; Length of the String str ; Create an empty compressed String ; The first character will always be included in the readonly String ; Check all the characters of the String ; If the current character is not same as its previous one , then insert it in the readonly String ; Check if the compressed String is a palindrome ; Driver Code ; Given String ; Function call
using System ; class GFG { static bool isPalindrome ( String str ) { int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] != str [ len - i - 1 ] ) return false ; } return true ; } static bool isCompressablePalindrome ( String str ) { int len = str . Length ; String compressed = " " ; compressed = String . Join ( " " , str [ 0 ] ) ; for ( int i = 1 ; i < len ; i ++ ) { if ( str [ i ] != str [ i - 1 ] ) compressed += str [ i ] ; } return isPalindrome ( compressed ) ; } public static void Main ( String [ ] args ) { String str = " abbcbbbaaa " ; if ( isCompressablePalindrome ( str ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } }
Count of substrings consisting only of vowels | C # program to count all substrings in a string which contains only vowels ; Function to check vowel or not ; Function to find largest string which satisfy condition ; Check if current character is vowel ; Increment count ; Count all possible substrings of calculated length ; Reset the length ; Add remaining possible substrings consisting of vowels occupying last indices of the string ; Driver code
using System ; class GFG { static bool isvowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } static int CountTotal ( string str ) { int ans = 0 ; int n = str . Length ; char [ ] s = str . ToCharArray ( ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isvowel ( s [ i ] ) ) cnt += 1 ; else { ans += ( cnt * ( cnt + 1 ) / 2 ) ; cnt = 0 ; } } if ( cnt != 0 ) ans += ( cnt * ( cnt + 1 ) / 2 ) ; return ans ; } static public void Main ( string [ ] args ) { string s = " geeksforgeeks " ; Console . Write ( CountTotal ( s ) ) ; } }
Number formed by flipping all bits to the left of rightmost set bit | C # program to find the integer formed after flipping all bits to the left of the rightmost set bit ; Function to get the total count ; Moving until we get the rightmost set bit ; To get total number of bits in a number ; Function to find the integer formed after flipping all bits to the left of the rightmost set bit ; Find the total count of bits and the rightmost set bit ; XOR given number with the number which has is made up of only totbits set ; To avoid flipping the bits to the right of the set bit , take XOR with the number made up of only set firstbits ; Driver Code
using System ; class GFG { static int totCount ; static int firstCount ; static void getTotCount ( int num ) { totCount = 1 ; firstCount = 1 ; int temp = 1 ; while ( ( num & temp ) == 0 ) { temp = temp << 1 ; totCount += 1 ; } firstCount = totCount ; temp = num >> totCount ; while ( temp != 0 ) { totCount += 1 ; temp = temp >> 1 ; } } static int flipBitsFromRightMostSetBit ( int num ) { getTotCount ( num ) ; int num1 = num ^ ( ( 1 << totCount ) - 1 ) ; num1 = num1 ^ ( ( 1 << firstCount ) - 1 ) ; return num1 ; } public static void Main ( string [ ] args ) { int n = 120 ; Console . Write ( flipBitsFromRightMostSetBit ( n ) ) ; } }
Longest substring of vowels with no two adjacent alphabets same | C # implementation of the above approach ; Function to check a character is vowel or not ; Function to find length of longest substring consisting only of vowels and no similar adjacent alphabets ; Stores max length of valid subString ; Stores length of current valid subString ; If curr and prev character are not same , include it ; If same as prev one , start new subString from here ; Store max in maxLen ; Driver code
using System ; public class GFG { public static bool isVowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } public static int findMaxLen ( string s ) { int maxLen = 0 ; int cur ; if ( isVowel ( s [ 0 ] ) ) maxLen = 1 ; cur = maxLen ; for ( int i = 1 ; i < s . Length ; i ++ ) { if ( isVowel ( s [ i ] ) ) { if ( s [ i ] != s [ i - 1 ] ) cur += 1 ; else cur = 1 ; } else { cur = 0 ; } maxLen = Math . Max ( cur , maxLen ) ; } return maxLen ; } public static void Main ( string [ ] args ) { string Str = " aeoibsddaeiouudb " ; Console . WriteLine ( findMaxLen ( Str ) ) ; } }
Count of non | C # program to count non - palindromic strings of length M using N distinct characters ; Iterative Function to calculate base ^ pow in O ( log y ) ; Function to return the count of non palindromic strings ; Count of strings using n characters with repetitions allowed ; Count of palindromic strings ; Count of non - palindromic strings ; Driver code
using System ; class GFG { static long power ( long Base , long pow ) { long res = 1 ; while ( pow > 0 ) { if ( ( pow & 1 ) == 1 ) res = ( res * Base ) ; Base = ( Base * Base ) ; pow >>= 1 ; } return res ; } static long countNonPalindromicString ( long n , long m ) { long total = power ( n , m ) ; long palindrome = power ( n , m / 2 + m % 2 ) ; long count = total - palindrome ; return count ; } public static void Main ( String [ ] args ) { int n = 3 , m = 5 ; Console . WriteLine ( countNonPalindromicString ( n , m ) ) ; } }
Count of K | C # program for the above approach ; To store the frequency array ; Function to check palindromic of of any subString using frequency array ; Initialise the odd count ; Traversing frequency array to compute the count of characters having odd frequency ; Returns true if odd count is atmost 1 ; Function to count the total number subString whose any permutations are palindromic ; Computing the frequency of first K character of the String ; To store the count of palindromic permutations ; Checking for the current window if it has any palindromic permutation ; Start and end point of window ; Decrementing count of first element of the window ; Incrementing count of next element of the window ; Checking current window character frequency count ; Return the final count ; Driver Code ; Given String str ; Window of size K ; Function Call
using System ; class GFG { static int [ ] freq = new int [ 26 ] ; static bool checkPalindrome ( ) { int oddCnt = 0 ; foreach ( int x in freq ) { if ( x % 2 == 1 ) oddCnt ++ ; } return oddCnt <= 1 ; } static int countPalindromePermutation ( char [ ] s , int k ) { int i = 0 ; for ( i = 0 ; i < k ; i ++ ) { freq [ s [ i ] - 97 ] ++ ; } int ans = 0 ; if ( checkPalindrome ( ) ) { ans ++ ; } int j = k ; i = 0 ; while ( j < s . Length ) { freq [ s [ i ++ ] - 97 ] -- ; freq [ s [ j ++ ] - 97 ] ++ ; if ( checkPalindrome ( ) ) { ans ++ ; } } return ans ; } public static void Main ( String [ ] args ) { String str = " abbaca " ; int K = 3 ; Console . Write ( countPalindromePermutation ( str . ToCharArray ( ) , K ) + " STRNEWLINE " ) ; } }
Minimum flips required to form given binary string where every flip changes all bits to its right as well | C # program for the above approach ; Function to return the count of minimum flips required ; If curr occurs in the readonly string ; Switch curr to '0' if '1' or vice - versa ; Driver code
using System ; class GFG { public static int minFlips ( String target ) { char curr = '1' ; int count = 0 ; for ( int i = 0 ; i < target . Length ; i ++ ) { if ( target [ i ] == curr ) { count ++ ; curr = ( char ) ( 48 + ( curr + 1 ) % 2 ) ; } } return count ; } public static void Main ( String [ ] args ) { String S = "011000" ; Console . WriteLine ( minFlips ( S ) ) ; } }
Check if a number ends with another number or not | C # program for the above approach ; Function to check if B is a suffix of A or not ; Find the number of digit in B ; Subtract B from A ; Returns true , if B is a suffix of A ; Driver code ; Given numbers ; Function call ; If B is a suffix of A , then print " Yes "
using System ; class GFG { static bool checkSuffix ( int A , int B ) { int digit_B = ( int ) ( Math . Log10 ( B ) + 1 ) ; A -= B ; return ( A % ( int ) ( Math . Pow ( 10 , digit_B ) ) > 0 ) ; } public static void Main ( ) { int A = 12345 , B = 45 ; bool result = checkSuffix ( A , B ) ; if ( ! result ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Minimum length of substring whose rotation generates a palindromic substring | C # Program to find the minimum length of substring whose rotation generates a palindromic substring ; Function to return the minimum length of substring ; Store the index of previous occurrence of the character ; Variable to store the maximum length of substring ; If the current character hasn 't appeared yet ; If the character has occured within one or two previous index , a palindromic substring already exists ; Update the maximum ; Replace the previous index of the character by the current index ; If character appeared at least twice ; Driver code
using System ; class GFG { static int count_min_length ( string s ) { int [ ] hash = new int [ 26 ] ; int ans = int . MaxValue ; for ( int i = 0 ; i < 26 ; i ++ ) hash [ i ] = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( hash [ s [ i ] - ' a ' ] == - 1 ) hash [ s [ i ] - ' a ' ] = i ; else { if ( hash [ s [ i ] - ' a ' ] == i - 1 hash [ s [ i ] - ' a ' ] == i - 2 ) return 0 ; ans = Math . Min ( ans , i - hash [ s [ i ] - ' a ' ] - 1 ) ; hash [ s [ i ] - ' a ' ] = i ; } } if ( ans == int . MaxValue ) return - 1 ; return ans ; } public static void Main ( string [ ] args ) { string str = " abcdeba " ; Console . WriteLine ( count_min_length ( str ) ) ; } }
Program to remove HTML tags from a given String | C # program for the above approach ; Function to remove the HTML tags from the given tags ; Use replaceAll function in regex to erase every tags enclosed in < > str = Regex . Replace ( str , " < . * ? > " , String . Empty ) ; Print string after removing tags ; Driver code ; Given String ; Function call to print the HTML string after removing tags
using System ; class GFG { static void RemoveHTMLTags ( String str ) { System . Text . RegularExpressions . Regex rx = new System . Text . RegularExpressions . Regex ( " < [ ^ > ] * > " ) ; str = rx . Replace ( str , " " ) ; Console . WriteLine ( str ) ; } public static void Main ( String [ ] args ) { String str ; str = " < div > < b > Geeks ▁ for ▁ Geeks < / b > < / div > " ; RemoveHTMLTags ( str ) ; } }
Score of Parentheses using Tree | C # program to find the score of parentheses using Tree ; Customized tree class or struct , contains all required methods . ; Function to add a child into the list of children ; Function to change the parent pointer to the node passed ; Function to return the parent of the current node ; Function to compute the score recursively . ; Base case ; Adds scores of all children ; Function to create the tree structure ; Creating a node for every " ( ) " ; If we find " ( " we add a node as a child ; On finding " ) " which confirms that a pair is closed , we go back to the parent ; Driver code ; Generating the tree ; Computing the score
using System ; using System . Collections ; class GFG { class TreeNode { public TreeNode parent = null ; public ArrayList children = new ArrayList ( ) ; public void addChild ( TreeNode node ) { children . Add ( node ) ; } public void setParent ( TreeNode node ) { parent = node ; } public TreeNode getParent ( ) { return parent ; } public int computeScore ( ) { if ( children . Count == 0 ) return 1 ; int res = 0 ; foreach ( TreeNode curr in children ) res += curr . computeScore ( ) ; if ( parent == null ) return res ; else return 2 * res ; } } ; static TreeNode computeTree ( string s ) { TreeNode current = new TreeNode ( ) ; TreeNode root = current ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' ( ' ) { TreeNode child = new TreeNode ( ) ; child . setParent ( current ) ; current . addChild ( child ) ; current = child ; } else { current = current . getParent ( ) ; } } return root ; } public static void Main ( ) { string s = " ( ( ) ( ( ) ) ) " ; TreeNode root = computeTree ( s ) ; Console . Write ( root . computeScore ( ) ) ; } }
Move all occurrence of letter ' x ' from the string s to the end using Recursion | C # program for the above approach ; Recursive program to bring ' x ' to the end ; When the string is completed from reverse direction end of recursion ; If the character x is found ; Transverse the whole string ; Swap the x so that it moves to the last ; call to the smaller problem now ; Driver code ; Size of a ; Call to rec
using System ; class GFG { static void rec ( char [ ] a , int i ) { if ( i == 0 ) { Console . WriteLine ( a ) ; return ; } if ( a [ i ] == ' x ' ) { int j = i ; while ( a [ j ] != ' \0' && a [ j + 1 ] != ' \0' ) { char temp = a [ j ] ; a [ j ] = a [ j + 1 ] ; a [ j + 1 ] = temp ; j ++ ; } } rec ( a , i - 1 ) ; } static void Main ( ) { char [ ] a = { ' g ' , ' e ' , ' e ' , ' k ' , ' x ' , ' s ' , ' x ' , ' x ' , ' k ' , ' s ' , ' \0' } ; int n = 10 ; rec ( a , n - 1 ) ; } }
Find the largest Alphabetic character present in the string | C # program to Find the Largest Alphabetic char present in the string of both uppercase and lowercase English characters ; Function to find the Largest Alphabetic char ; Array for keeping track of both uppercase and lowercase english alphabets ; Iterate from right side of array to get the largest index character ; Check for the character if both its uppercase and lowercase exist or not ; Return - 1 if no such character whose uppercase and lowercase present in string str ; Driver code
using System ; class GFG { public static String largestchar ( String str ) { bool [ ] uppercase = new bool [ 26 ] ; bool [ ] lowercase = new bool [ 26 ] ; char [ ] arr = str . ToCharArray ( ) ; foreach ( char c in arr ) { if ( char . IsLower ( c ) ) lowercase = true ; if ( char . IsUpper ( c ) ) uppercase = true ; } for ( int i = 25 ; i >= 0 ; i -- ) { if ( uppercase [ i ] && lowercase [ i ] ) return ( char ) ( i + ' A ' ) + " " ; } return " - 1" ; } public static void Main ( String [ ] args ) { String str = " admeDCAB " ; Console . WriteLine ( largestchar ( str ) ) ; } }