codeparrot/xlcost-text-to-code huggingface dataset:
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 check a substring is valid or not ; If size is 1 then check only first character ; If 0 'th character is  not vowel then invalid ; If two adjacent characters are same or i 'th char is  not vowel then invalid ; Function to find length of longest substring consisting only of vowels and no similar adjacent alphabets ; Stores max length of valid substring ; For current substring ; Check if substring is valid ; Size of substring is ( j - i + 1 ) ; Driver code
using System ; class GFG { static bool isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static bool isValid ( string s ) { int n = s . Length ; if ( n == 1 ) return ( isVowel ( s [ 0 ] ) ) ; if ( isVowel ( s [ 0 ] ) == false ) return false ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] || ! isVowel ( s [ i ] ) ) return false ; } return true ; } static int findMaxLen ( string s ) { int maxLen = 0 ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { string temp = " " ; for ( int j = i ; j < n ; j ++ ) { temp = temp + s [ j ] ; if ( isValid ( temp ) ) maxLen = Math . Max ( maxLen , ( j - i + 1 ) ) ; } } return maxLen ; } public static void Main ( ) { string Str = " aeoibsddaeiouudb " ; Console . WriteLine ( findMaxLen ( Str ) ) ; } }
Check if the number is balanced | C # program to check if a number is Balanced or not ; Function to check whether N is Balanced Number or not ; Calculating the Leftsum and rightSum simultaneously ; Typecasting each character to integer and adding the digit to respective sums ; Driver code ; Function call
using System ; class GFG { static void BalancedNumber ( string s ) { int Leftsum = 0 ; int Rightsum = 0 ; for ( int i = 0 ; i < s . Length / 2 ; i ++ ) { Leftsum += ( int ) ( Char . GetNumericValue ( s [ i ] ) - Char . GetNumericValue ( '0' ) ) ; Rightsum += ( int ) ( Char . GetNumericValue ( s [ s . Length - 1 - i ] ) - Char . GetNumericValue ( '0' ) ) ; } if ( Leftsum == Rightsum ) Console . WriteLine ( " Balanced " ) ; else Console . WriteLine ( " Not ▁ Balanced " ) ; } static void Main ( ) { string s = "12321" ; BalancedNumber ( s ) ; } }
Count of N size strings consisting of at least one vowel and one consonant | C # program to count all possible Strings of length N consisting of atleast one vowel and one consonant ; Function to return base ^ exponent ; Function to count all possible Strings ; All possible Strings of length N ; Vowels only ; Consonants only ; Return the readonly result ; Driver code
using System ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int expo ( int Base , int exponent ) { int ans = 1 ; while ( exponent != 0 ) { if ( ( exponent & 1 ) == 1 ) { ans = ans * Base ; ans = ans % mod ; } Base = Base * Base ; Base %= mod ; exponent >>= 1 ; } return ans % mod ; } static int findCount ( int N ) { int ans = ( expo ( 26 , N ) - expo ( 5 , N ) - expo ( 21 , N ) ) % mod ; ans += mod ; ans %= mod ; return ans ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . Write ( findCount ( N ) ) ; } }
Sum of decomposition values of all suffixes of an Array | C # implementation to find the sum of Decomposition values of all suffixes of an array ; Function to find the decomposition values of the array ; Stack ; Variable to maintain min value in stack ; Loop to iterate over the array ; Condition to check if the stack is empty ; Condition to check if the top of the stack is greater than the current element ; Loop to pop the element out ; The size of the stack is the max no of subarrays for suffix till index i from the right ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int decompose ( List < int > S ) { Stack < int > s = new Stack < int > ( ) ; int N = S . Count ; int ans = 0 ; int nix = Int32 . MaxValue ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( s . Count == 0 ) { s . Push ( S [ i ] ) ; nix = S [ i ] ; } else { if ( S [ i ] < s . Peek ( ) ) { s . Push ( S [ i ] ) ; nix = Math . Min ( nix , S [ i ] ) ; } else { int val = S [ i ] ; while ( s . Count != 0 && val >= s . Peek ( ) ) { s . Pop ( ) ; } nix = Math . Min ( nix , S [ i ] ) ; s . Push ( nix ) ; } } ans += s . Count ; } return ans ; } static void Main ( ) { List < int > S = new List < int > ( ) ; S . Add ( 9 ) ; S . Add ( 6 ) ; S . Add ( 9 ) ; S . Add ( 35 ) ; Console . WriteLine ( decompose ( S ) ) ; } }
Maximum number of set bits count in a K | C # program to find the maximum set bits in a substring of size K ; Function that find Maximum number of set bit appears in a substring of size K . ; Traverse string 1 to k ; Increment count if character is set bit ; Traverse string k + 1 to length of string ; remove the contribution of the ( i - k ) th character which is no longer in the window ; add the contribution of the current character ; update maxCount at for each window of size k ; return maxCount ; Driver Program
using System ; class GFG { static int maxSetBitCount ( string s , int k ) { int maxCount = 0 , n = s . Length ; int count = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( s [ i ] == '1' ) count ++ ; } maxCount = count ; for ( int i = k ; i < n ; i ++ ) { if ( s [ i - k ] == '1' ) count -- ; if ( s [ i ] == '1' ) count ++ ; maxCount = Math . Max ( maxCount , count ) ; } return maxCount ; } public static void Main ( ) { string s = "100111010" ; int k = 3 ; Console . Write ( maxSetBitCount ( s , k ) ) ; } }
Minimum characters to be deleted from the beginning of two strings to make them equal | C # Program to count minimum number of characters to be deleted from the beginning of the two strings to make the strings equal ; Function that finds minimum character required to be deleted ; Iterate in the strings ; Check if the characters are not equal ; Return the result ; Driver Code
using System ; class GFG { static int minDel ( string s1 , string s2 ) { int i = s1 . Length ; int j = s2 . Length ; while ( i > 0 && j > 0 ) { if ( s1 [ i - 1 ] != s2 [ j - 1 ] ) { break ; } i -- ; j -- ; } return i + j ; } public static void Main ( ) { string s1 = " geeksforgeeks " , s2 = " peeks " ; Console . Write ( minDel ( s1 , s2 ) ) ; } }
Minimum characters to be deleted from the end to make given two strings equal | C # program to count minimum number of characters to be deleted to make the strings equal ; Function that finds minimum character required to be deleted ; Iterate in the strings ; Check if the characters are not equal ; Return the result ; Driver code
using System ; class GFG { static int minDel ( String s1 , String s2 ) { int i = 0 ; while ( i < Math . Min ( s1 . Length , s2 . Length ) ) { if ( s1 [ i ] != s2 [ i ] ) { break ; } i ++ ; } int ans = ( ( s1 . Length - i ) + ( s2 . Length - i ) ) ; return ans ; } public static void Main ( String [ ] args ) { String s1 = " geeks " ; String s2 = " geeksfor " ; Console . WriteLine ( minDel ( s1 , s2 ) ) ; } }
Count of triplets from the given string with non | C # program to count of triplets from the given string with non - equidistant characters ; Function to count valid triplets ; Store frequencies of a , b and c ; If the current letter is ' a ' ; If the current letter is ' b ' ; If the current letter is ' c ' ; Calculate total no of triplets ; Subtract invalid triplets ; Driver code
using System ; class GFG { static void CountValidTriplet ( string s , int n ) { int count_a = 0 , count_b = 0 , count_c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' ) count_a ++ ; if ( s [ i ] == ' b ' ) count_b ++ ; if ( s [ i ] == ' c ' ) count_c ++ ; } int Total_triplet = count_a * count_b * count_c ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( 2 * j - i ) < n && s [ j ] != s [ i ] && s [ j * 2 - i ] != s [ j ] && s [ 2 * j - i ] != s [ i ] ) Total_triplet -- ; } } Console . Write ( Total_triplet ) ; } public static void Main ( ) { string s = " abcbcabc " ; int n = s . Length ; CountValidTriplet ( s , n ) ; } }
Check if given Parentheses expression is balanced or not | C # program for the above approach . ; Function to check if parentheses are balanced ; Initialising variables ; Traversing the expression ; It is a closing parenthesis ; This means there are more Closing parenthesis than opening ones ; If count is not zero , It means there are more opening parenthesis ; Driver code
using System ; class GFG { public static bool isBalanced ( String exp ) { bool flag = true ; int count = 0 ; for ( int i = 0 ; i < exp . Length ; i ++ ) { if ( exp [ i ] == ' ( ' ) { count ++ ; } else { count -- ; } if ( count < 0 ) { flag = false ; break ; } } if ( count != 0 ) { flag = false ; } return flag ; } public static void Main ( String [ ] args ) { String exp1 = " ( ( ( ) ) ) ( ) ( ) " ; if ( isBalanced ( exp1 ) ) Console . WriteLine ( " Balanced " ) ; else Console . WriteLine ( " Not ▁ Balanced " ) ; String exp2 = " ( ) ) ( ( ( ) ) " ; if ( isBalanced ( exp2 ) ) Console . WriteLine ( " Balanced " ) ; else Console . WriteLine ( " Not ▁ Balanced " ) ; } }
Longest Palindromic Subsequence of two distinct characters | C # implementation to find longest Palindromic Subsequence consisting of two distinct characters only ; Function that prints the length of maximum required subsequence ; Calculate length of String ; Store number of occurrences of each character and position of each chatacter in String ; Iterate all characters ; Calculate number of occurences of the current character ; Iterate half of the number of positions of current character ; Determine maximum length of a character between l and r position ; Compute the maximum from all ; Printing maximum length ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void longestPalindrome ( String s ) { int n = s . Length ; int [ , ] pref = new int [ 26 , n ] ; List < int > [ ] pos = new List < int > [ 26 ] ; for ( int i = 0 ; i < pos . Length ; i ++ ) pos [ i ] = new List < int > ( ) ; pref [ s [ 0 ] - ' a ' , 0 ] ++ ; pos [ s [ 0 ] - ' a ' ] . Add ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) pref [ j , i ] += pref [ j , i - 1 ] ; int index = s [ i ] - ' a ' ; pref [ index , i ] ++ ; pos [ index ] . Add ( i ) ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int size = pos [ i ] . Count ; ans = Math . Max ( ans , size ) ; for ( int j = 0 ; j < size / 2 ; j ++ ) { int l = pos [ i ] [ j ] ; int r = pos [ i ] [ size - j - 1 ] - 1 ; for ( int k = 0 ; k < 26 ; k ++ ) { int sum = pref [ k , r ] - pref [ k , l ] ; ans = Math . Max ( ans , 2 * ( j + 1 ) + sum ) ; } } } Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String S = " bbccdcbb " ; longestPalindrome ( S ) ; } }
Check if a given string can be formed using characters of adjacent cells of a Matrix | C # program to check if a given word can be formed from the adjacent characters in a matrix of characters ; Function to check if the word exists ; If index exceeds board range ; If the current cell does not contain the required character ; If the cell contains the required character and is the last character of the word required to be matched ; Return true as word is found ; Mark cell visited ; Check adjacent cells for the next character ; Restore cell value ; Driver Code
using System ; class GFG { static bool checkWord ( char [ , ] board , String word , int index , int row , int col ) { if ( row < 0 || col < 0 || row >= board . GetLength ( 0 ) || col >= board . GetLength ( 1 ) ) return false ; if ( board [ row , col ] != word [ index ] ) return false ; else if ( index == word . Length - 1 ) return true ; char temp = board [ row , col ] ; board [ row , col ] = ' * ' ; if ( checkWord ( board , word , index + 1 , row + 1 , col ) || checkWord ( board , word , index + 1 , row - 1 , col ) || checkWord ( board , word , index + 1 , row , col + 1 ) || checkWord ( board , word , index + 1 , row , col - 1 ) ) { board [ row , col ] = temp ; return true ; } board [ row , col ] = temp ; return false ; } public static void Main ( String [ ] args ) { char [ , ] board = { { ' A ' , ' B ' , ' C ' , ' E ' } , { ' S ' , ' F ' , ' C ' , ' S ' } , { ' A ' , ' D ' , ' E ' , ' E ' } } ; String word = " CFDASABCESEE " ; for ( int i = 0 ; i < board . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < board . GetLength ( 1 ) ; j ++ ) { if ( board [ i , j ] == word [ 0 ] && checkWord ( board , word , 0 , i , j ) ) { Console . WriteLine ( " True " ) ; return ; } } } Console . WriteLine ( " False " ) ; } }
Split the number N by maximizing the count of subparts divisible by K | C # program to split the number N by maximizing the count of subparts divisible by K ; Function to count the subparts ; Total subStr till now ; If it can be divided , then this subString is one of the possible answer ; Convert String to long and check if its divisible with X ; Consider there is no vertical cut between this index and the next one , hence take total carrying total substr a . ; If there is vertical cut between this index and next one , then we start again with subStr as " " and add b for the count of subStr upto now ; Return max of both the cases ; Driver code
using System ; class GFG { static int count ( String N , int X , String subStr , int index , int n ) { if ( index == n ) return 0 ; String a = subStr + N [ index ] ; int b = 0 ; if ( long . Parse ( a ) % X == 0 ) b = 1 ; int m1 = count ( N , X , a , index + 1 , n ) ; int m2 = b + count ( N , X , " " , index + 1 , n ) ; return Math . Max ( m1 , m2 ) ; } public static void Main ( String [ ] args ) { String N = "00001242" ; int K = 3 ; int l = N . Length ; Console . Write ( count ( N , K , " " , 0 , l ) + " STRNEWLINE " ) ; } }
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 ; Convert numbers into strings ; Find the lengths of strings s1 and s2 ; Base case ; Traverse the strings s1 & s2 ; If at any index characters are unequals then return false ; Return true ; Driver code ; Given numbers ; Function Call ; If B is a suffix of A , then print " Yes "
using System ; class GFG { public static bool checkSuffix ( int A , int B ) { string s1 = A . ToString ( ) ; string s2 = B . ToString ( ) ; int n1 = s1 . Length ; int n2 = s2 . Length ; if ( n1 < n2 ) { return false ; } for ( int i = 0 ; i < n2 ; i ++ ) { if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) { return false ; } } return true ; } public static void Main ( string [ ] args ) { int A = 12345 , B = 45 ; bool result = checkSuffix ( A , B ) ; if ( result ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
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 ; Convert numbers into Strings ; Check if s2 is a suffix of s1 or not ; If result is true , print " Yes " ; Driver Code ; Given numbers ; Function Call
using System ; class GFG { static void checkSuffix ( int A , int B ) { String s1 = String . Join ( " " , A ) ; String s2 = String . Join ( " " , B ) ; bool result ; result = s1 . EndsWith ( s2 ) ; if ( result ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( String [ ] args ) { int A = 12345 , B = 45 ; checkSuffix ( A , B ) ; } }
Binary string with given frequencies of sums of consecutive pairs of characters | C # program to generate a binary String with given frequencies of sums of consecutive pair of characters ; A Function that generates and returns the binary String ; P : Frequency of consecutive characters with sum 0 Q : Frequency of consecutive characters with sum 1 R : Frequency of consecutive characters with sum 2 ; If no consecutive character adds up to 1 ; Not possible if both P and Q are non - zero ; If P is not equal to 0 ; Append 0 P + 1 times ; Append 1 R + 1 times ; Append "01" to satisfy Q ; Append "0" P times ; Append "1" R times ; Driver Code
using System ; class GFG { static String build_binary_str ( int p , int q , int r ) { String ans = " " ; if ( q == 0 ) { if ( p != 0 && r != 0 ) { return " - 1" ; } else { if ( p > 0 ) { ans = Strings ( p + 1 , '0' ) ; } else { ans = Strings ( r + 1 , '1' ) ; } } } else { for ( int i = 1 ; i <= q + 1 ; i ++ ) { if ( i % 2 == 0 ) { ans += '0' ; } else { ans += '1' ; } } ans = ans . Substring ( 0 , 1 ) + Strings ( p , '0' ) + ans . Substring ( 1 ) ; ans = Strings ( r , '1' ) + ans ; } return ans ; } static String Strings ( int p , char c ) { String ans = " " ; for ( int i = 0 ; i < p ; i ++ ) ans += c ; return ans ; } public static void Main ( String [ ] args ) { int p = 1 , q = 2 , r = 2 ; Console . Write ( build_binary_str ( p , q , r ) ) ; } }
Check if there exists a permutation of given string which doesn 't contain any monotonous substring | C # implementation such that there are no monotonous string in given string ; Function to check a string doesn 't contains a monotonous substring ; Loop to iterate over the string and check that it doesn 't contains  the monotonous substring ; Function to check that there exist a arrangement of string such that it doesn 't contains monotonous substring ; Loop to group the characters of the string into two buckets ; Sorting the two buckets ; Condition to check if the concatenation point doesn 't  contains the monotonous string ; Driver Code
using System ; class GFG { static bool check ( string s ) { bool ok = true ; for ( int i = 0 ; i + 1 < s . Length ; ++ i ) ok &= ( Math . Abs ( s [ i ] - s [ i + 1 ] ) != 1 ) ; return ok ; } static string monotonousString ( string s ) { string odd = " " , even = " " ; for ( int i = 0 ; i < s . Length ; ++ i ) { if ( s [ i ] % 2 == 0 ) odd += s [ i ] ; else even += s [ i ] ; } char [ ] oddArray = odd . ToCharArray ( ) ; Array . Sort ( oddArray ) ; odd = new String ( oddArray ) ; char [ ] evenArray = even . ToCharArray ( ) ; Array . Sort ( evenArray ) ; even = new String ( evenArray ) ; if ( check ( odd + even ) ) return " Yes " ; else if ( check ( even + odd ) ) return " Yes " ; return " No " ; } public static void Main ( string [ ] args ) { string str = " abcd " ; string ans ; ans = monotonousString ( str ) ; Console . Write ( ans ) ; } }
Length of the smallest substring which contains all vowels | C # program to find the length of the smallest subString of which contains all vowels ; Function to return the index for respective vowels to increase their count ; Returns - 1 for consonants ; Function to find the minimum length ; Store the starting index of the current subString ; Store the frequencies of vowels ; If the current character is a vowel ; Increase its count ; Move start as much right as possible ; Condition for valid subString ; Driver code
using System ; class GFG { static int get_index ( char ch ) { if ( ch == ' a ' ) return 0 ; else if ( ch == ' e ' ) return 1 ; else if ( ch == ' i ' ) return 2 ; else if ( ch == ' o ' ) return 3 ; else if ( ch == ' u ' ) return 4 ; else return - 1 ; } static int findMinLength ( String s ) { int n = s . Length ; int ans = n + 1 ; int start = 0 ; int [ ] count = new int [ 5 ] ; for ( int x = 0 ; x < n ; x ++ ) { int idx = get_index ( s [ x ] ) ; if ( idx != - 1 ) { count [ idx ] ++ ; } int idx_start = get_index ( s [ start ] ) ; while ( idx_start == - 1 count [ idx_start ] > 1 ) { if ( idx_start != - 1 ) { count [ idx_start ] -- ; } start ++ ; if ( start < n ) idx_start = get_index ( s [ start ] ) ; } if ( count [ 0 ] > 0 && count [ 1 ] > 0 && count [ 2 ] > 0 && count [ 3 ] > 0 && count [ 4 ] > 0 ) { ans = Math . Min ( ans , x - start + 1 ) ; } } if ( ans == n + 1 ) return - 1 ; return ans ; } public static void Main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; Console . Write ( findMinLength ( s ) ) ; } }
Number of strings which starts and ends with same character after rotations | C # program for the above approach ; Function to find the count of string with equal end after rotations ; To store the final count ; Traverse the string ; If current character is same as the previous character then increment the count ; Return the final count ; Driver Code ; Given string ; Function call
using System ; class GFG { static int countStrings ( String s ) { int cnt = 0 ; for ( int i = 1 ; i < s . Length - 1 ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { cnt ++ ; } } return cnt ; } public static void Main ( String [ ] args ) { String str = " aacbb " ; Console . WriteLine ( countStrings ( str ) ) ; } }
Check if frequency of each character is equal to its position in English Alphabet | C # program for the above approach ; Initialise frequency array ; Traverse the String ; Update the frequency ; Check for valid String ; If frequency is non - zero ; If freq is not equals to ( i + 1 ) , then return false ; Return true ; ; Driver Code ; Given String str
using System ; class GFG { static bool checkValidString ( String str ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < str . Length ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 ) { if ( freq [ i ] != i + 1 ) { return false ; } } } return true ; } public static void Main ( String [ ] args ) { String str = " abbcccdddd " ; if ( checkValidString ( str ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Print all the non | C # program to print all the non - repeating words from the two given sentences ; Function to print all the non - repeating words from the two given sentences ; Concatenate the two Strings into one ; Iterating over the whole concatenated String ; Searching for the word in A . If while searching , we reach the end of the String , then the word is not present in the String ; Initialise word for the next iteration ; Driver code
using System ; class GFG { static void removeRepeating ( string s1 , string s2 ) { string s3 = s1 + " ▁ " + s2 + " ▁ " ; string words = " " ; int i = 0 ; foreach ( char x in s3 . ToCharArray ( ) ) { if ( x == ' ▁ ' ) { if ( ! s1 . Contains ( words ) || ! s2 . Contains ( words ) ) Console . Write ( words ) ; words = " ▁ " ; } else { words = words + x ; } } } public static void Main ( string [ ] args ) { string s1 = " I ▁ have ▁ go ▁ a ▁ pen " ; string s2 = " I ▁ want ▁ to ▁ go ▁ park " ; removeRepeating ( s1 , s2 ) ; } }