codeparrot/xlcost-text-to-code huggingface dataset:
Find the GCD of an array made up of numeric strings | C # program for the above approach ; Recursive function to return gcd of A and B ; Base case ; Length of A is greater ; Calculate GCD ; Store the GCD of the length of the Strings ; Driver Code ; Function call
using System ; class GFG { static int GCD ( int lena , int lenb ) { if ( lena == 0 ) return lenb ; if ( lenb == 0 ) return lena ; if ( lena == lenb ) return lena ; if ( lena > lenb ) return GCD ( lena - lenb , lenb ) ; return GCD ( lena , lenb - lena ) ; } static String StringGCD ( String a , String b ) { int gcd = GCD ( a . Length , b . Length ) ; if ( a . Substring ( 0 , gcd ) . Equals ( b . Substring ( 0 , gcd ) ) ) { int x = ( ( int ) b . Length / gcd ) ; int y = ( ( int ) a . Length / gcd ) ; String r = " " , s = " " ; while ( x -- > 0 ) s += a ; while ( y -- > 0 ) r += b ; if ( s . Equals ( r ) ) return a . Substring ( 0 , gcd ) ; } return " - 1" ; } public static void Main ( String [ ] args ) { String a = " geeksgeeks " ; String b = " geeks " ; Console . Write ( StringGCD ( a , b ) ) ; } }
Calculate score of a string consisting of balanced parentheses | C # program to implement the above approach ; Function to calculate score of parentheses ; Stores index of character of String ; Stores total scores obtained from the String ; Iterate over characters of the String ; If s [ i ] is ' ( ' ; If top element of stack is ' ( ' ; Stores score of inner parentheses ; Calculate score of inner parentheses ; Update count ; Pop from stack ; Insert score of inner parentheses ; Update i ; Calculate score of the String ; Update ans ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static long scoreOfParentheses ( String S ) { Stack < String > s = new Stack < String > ( ) ; int i = 0 ; long ans = 0 ; while ( i < S . Length ) { if ( S [ i ] == ' ( ' ) s . Push ( " ( " ) ; else { if ( s . Peek ( ) == " ( " ) { s . Pop ( ) ; s . Push ( "1" ) ; } else { long count = 0 ; while ( s . Peek ( ) != " ( " ) { count += Int32 . Parse ( s . Peek ( ) ) ; s . Pop ( ) ; } s . Pop ( ) ; s . Push ( String . Join ( " " , 2 * count ) ) ; } } i ++ ; } while ( s . Count != 0 ) { ans += Int32 . Parse ( s . Peek ( ) ) ; s . Pop ( ) ; } return ans ; } public static void Main ( String [ ] args ) { String S1 = " ( ( ) ( ( ) ) ) " ; Console . Write ( scoreOfParentheses ( S1 ) + " STRNEWLINE " ) ; } }
Minimize cost to rearrange substrings to convert a string to a Balanced Bracket Sequence | C # program for the above approach ; Function to count minimum number of operations to convert the string to a balanced bracket sequence ; Initialize the integer array ; Traverse the string ; Decrement a [ i ] ; Increment a [ i ] ; Update the sum as current value of arr [ i ] ; If answer exists ; Traverse from i ; Find all substrings with 0 sum ; Print the sum of sizes ; No answer exists ; Driver Code
using System ; class GFG { static void countMinMoves ( string str ) { int n = str . Length ; int [ ] a = new int [ n ] ; int j , ans = 0 , i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ) ' ) { a [ i ] += sum - 1 ; } else { a [ i ] += sum + 1 ; } sum = a [ i ] ; } if ( sum == 0 ) { i = 1 ; while ( i < n ) { j = i - 1 ; while ( i < n && a [ i ] != 0 ) i ++ ; if ( i < n && a [ i - 1 ] < 0 ) { ans += i - j ; if ( j == 0 ) ans ++ ; } i ++ ; } Console . WriteLine ( ans ) ; } else Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { string str = " ) ( ( ) " ; countMinMoves ( str ) ; } }
Check if a given string can be converted to a Balanced Bracket Sequence | C # program for the above approach ; Function to check if the String can be balanced by replacing the ' $ ' with opening or closing brackets ; If String can never be balanced ; Declare 2 stacks to check if all ) can be balanced with ( or $ and vice - versa ; Store the count the occurence of ( , ) and $ ; Traverse the String ; Increment closed bracket count by 1 ; If there are no opening bracket to match it then return false ; Otherwise , pop the character from the stack ; If current character is an opening bracket or $ , push it to the stack ; Increment symbol count by 1 ; Increment open bracket count by 1 ; Traverse the String from end and repeat the same process ; If there are no closing brackets to match it ; Otherwise , pop character from stack ; Store the extra ( or ) which are not balanced yet ; Check if $ is available to balance the extra brackets ; Count ramaining $ after balancing extra ( and ) ; Check if each pair of $ is convertable in ( ) ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool canBeBalanced ( String sequence ) { if ( sequence . Length % 2 == 1 ) return false ; Stack < char > stack_ = new Stack < char > ( ) ; Stack < char > stack2_ = new Stack < char > ( ) ; int countOpen = 0 , countClosed = 0 ; int countSymbol = 0 ; for ( int i = 0 ; i < sequence . Length ; i ++ ) { if ( sequence [ i ] == ' ) ' ) { countClosed ++ ; if ( stack_ . Count == 0 ) { return false ; } else { stack_ . Pop ( ) ; } } else { if ( sequence [ i ] == ' $ ' ) { countSymbol ++ ; } else { countOpen ++ ; } stack_ . Push ( sequence [ i ] ) ; } } for ( int i = sequence . Length - 1 ; i >= 0 ; i -- ) { if ( sequence [ i ] == ' ( ' ) { if ( stack2_ . Count == 0 ) { return false ; } else { stack2_ . Pop ( ) ; } } else { stack2_ . Push ( sequence [ i ] ) ; } } int extra = Math . Abs ( countClosed - countOpen ) ; if ( countSymbol < extra ) { return false ; } else { countSymbol -= extra ; if ( countSymbol % 2 == 0 ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { String S = " ( ) ( $ " ; if ( canBeBalanced ( S ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | C # program for the above approach ; Function to find difference between maximum and minimum frequency of a character in given range ; Stores length of string ; Stores count of queries ; Iterate over the characters of the string ; Stores l - value of a query ; Stores r - value of a query ; Store count of every character laying in range [ l , r ] ; Update frequency of current character ; Stores maximum frequency of characters in given range ; Stores minimum frequency of characters in given range ; Iterate over all possible characters of the given string ; Update mx ; If ( j + ' a ' ) is present ; difference between max and min ; Driver code ; Given string ; Given queries ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void maxDiffFreq ( List < Tuple < int , int > > queries , string S ) { int N = S . Length ; int Q = queries . Count ; for ( int i = 0 ; i < Q ; ++ i ) { int l = queries [ i ] . Item1 - 1 ; int r = queries [ i ] . Item2 - 1 ; int [ ] freq = new int [ 26 ] ; for ( int j = l ; j <= r ; j ++ ) { freq [ S [ j ] - ' a ' ] ++ ; } int mx = 0 ; int mn = 99999999 ; for ( int j = 0 ; j < 26 ; j ++ ) { mx = Math . Max ( mx , freq [ j ] ) ; if ( freq [ j ] != 0 ) mn = Math . Min ( mn , freq [ j ] ) ; } Console . WriteLine ( mx - mn ) ; } } static void Main ( ) { string S = " abaabac " ; List < Tuple < int , int > > queries = new List < Tuple < int , int > > ( ) ; queries . Add ( new Tuple < int , int > ( 2 , 6 ) ) ; queries . Add ( new Tuple < int , int > ( 1 , 7 ) ) ; maxDiffFreq ( queries , S ) ; } }
Count strings from given array having all characters appearing in a given string | C # program to implement the above approach ; Function to count the number of Strings from an array having all characters appearing in the String S ; Initialize a set to store all distinct characters of String S ; Traverse over String S ; Insert characters into the Set ; Stores the required count ; Traverse the array ; Traverse over String arr [ i ] ; Check if character in arr [ i , j ] is present in the String S or not ; Increment the count if all the characters of arr [ i ] are present in the String S ; Finally , print the count ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countStrings ( String S , String [ ] list ) { HashSet < char > valid = new HashSet < char > ( ) ; foreach ( char x in S . ToCharArray ( ) ) { valid . Add ( x ) ; } int cnt = 0 ; for ( int i = 0 ; i < list . Length ; i ++ ) { int j = 0 ; for ( j = 0 ; j < list [ i ] . Length ; j ++ ) { if ( valid . Contains ( list [ i ] [ j ] ) ) continue ; else break ; } if ( j == list [ i ] . Length ) cnt ++ ; } return cnt ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ab " , " aab " , " abaaaa " , " bbd " } ; String S = " ab " ; Console . Write ( countStrings ( S , arr ) + " STRNEWLINE " ) ; } }
Minimize a string by removing all occurrences of another string | C # program for the above approach ; Function to find the minimum length to which String str can be reduced to by removing all occurrences of String K ; Initialize stack of characters ; Push character into the stack ; If stack size >= K . Count ; Create empty String to store characters of stack ; Traverse the String K in reverse ; If any of the characters differ , it means that K is not present in the stack ; Push the elements back into the stack ; Store the String ; Remove top element ; Size of stack gives the minimized length of str ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int minLength ( String str , int N , String K , int M ) { Stack < char > stackOfChar = new Stack < char > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { stackOfChar . Push ( str [ i ] ) ; if ( stackOfChar . Count >= M ) { String l = " " ; for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( K [ j ] != stackOfChar . Peek ( ) ) { int f = 0 ; while ( f != l . Length ) { stackOfChar . Push ( l [ f ] ) ; f ++ ; } break ; } else { l = stackOfChar . Peek ( ) + l ; stackOfChar . Pop ( ) ; } } } } return stackOfChar . Count ; } public static void Main ( String [ ] args ) { String S1 = " fffoxoxoxfxo " ; String S2 = " fox " ; int N = S1 . Length ; int M = S2 . Length ; Console . Write ( minLength ( S1 , N , S2 , M ) ) ; } }
Largest palindrome not exceeding N which can be expressed as product of two 3 | C # program for the above approach ; Function to find the largest palindrome not exceeding N which can be expressed as the product of two 3 - digit numbers ; Stores all palindromes ; Stores the product ; Check if X is palindrome ; Check n is less than N ; If true , append it in the list ; Print the largest palindrome ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void palindrome_prod ( String N ) { List < int > palindrome_list = new List < int > ( ) ; for ( int i = 101 ; i < 1000 ; i ++ ) { for ( int j = 121 ; j < 1000 ; j += ( i % 11 == 0 ) ? 1 : 11 ) { int n = i * j ; String x = String . Join ( " " , n ) ; String y = x ; reverse ( y ) ; if ( x == y ) { if ( n < Int32 . Parse ( N ) ) { palindrome_list . Add ( i * j ) ; } } } } Console . Write ( palindrome_list . Max ( ) ) ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String N = "101110" ; palindrome_prod ( N ) ; } }
Check if given strings can be made same by swapping two characters of same or different strings | C # program to implement the above approach ; Function to check if all strings can be made equal by swapping any pair of characters from the same or different strings ; Stores length of string ; Store frequency of each distinct character of the strings ; Traverse the array ; Iterate over the characters ; Update frequency of arr [ i ] [ j ] ; Traverse the array cntFreq [ ] ; If cntFreq [ i ] is divisible by N or not ; Driver Code ; Function Call
using System ; class GFG { static bool isEqualStrings ( string [ ] arr , int N ) { int M = arr [ 0 ] . Length ; int [ ] cntFreq = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { cntFreq [ i ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { cntFreq [ arr [ i ] [ j ] - ' a ' ] += 1 ; } } for ( int i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % N != 0 ) { return false ; } } return true ; } public static void Main ( ) { string [ ] arr = { " aab " , " bbc " , " cca " } ; int N = arr . Length ; if ( isEqualStrings ( arr , N ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
Minimize a binary string by repeatedly removing even length substrings of same characters | C # program for the above approach ; Recursive function to print stack elements from bottom to top without changing their order ; If stack is empty ; Pop top element of the stack ; Recursively call the function PrintStack ; Print the stack element from the bottom ; Push the same element onto the stack to preserve the order ; Function to minimize binary String by removing subStrings consisting of same character ; Declare a stack of characters ; Push the first character of the String into the stack ; Traverse the String s ; If Stack is empty ; Push current character into the stack ; Check if the current character is same as the top of the stack ; If true , pop the top of the stack ; Otherwise , push the current element ; Print stack from bottom to top ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void PrintStack ( Stack < char > s ) { if ( s . Count == 0 ) return ; char x = s . Peek ( ) ; s . Pop ( ) ; PrintStack ( s ) ; Console . Write ( ( char ) x ) ; s . Push ( x ) ; } static void minString ( String s ) { Stack < char > Stack = new Stack < char > ( ) ; Stack . Push ( s [ 0 ] ) ; for ( int i = 1 ; i < s . Length ; i ++ ) { if ( Stack . Count == 0 ) { Stack . Push ( s [ i ] ) ; } else { if ( Stack . Peek ( ) == s [ i ] ) { Stack . Pop ( ) ; } else { Stack . Push ( s [ i ] ) ; } } } PrintStack ( Stack ) ; } public static void Main ( String [ ] args ) { String str = "101001" ; minString ( str ) ; } }
Probability of collision between two trucks | C # program for the above approach ; Function to calculate total number of accidents ; String size ; Function to calculate count of all possible collision ; Stores the count of collisions ; Total number of truck in lane b ; Count total number of collisions while traversing the String a ; Function to calculate the probability of collisions ; Evaluate total outcome that is all the possible accident ; Evaluate favourable outcome i . e . , count of collision of trucks ; Print desired probability ; Driver Code ; Function Call
using System ; class GFG { static int count_of_accident ( String a , String b ) { int n = a . Length , m = b . Length ; if ( n > m ) return ( m * ( m + 1 ) ) / 2 ; else return ( n * ( n + 1 ) ) / 2 + ( m - n ) * n ; } static double count_of_collision ( String a , String b ) { int n = a . Length , m = b . Length ; double answer = 0 ; int count_of_truck_in_lane_b = 0 ; for ( int i = 0 ; i < m ; i ++ ) if ( b [ i ] == ' T ' ) count_of_truck_in_lane_b ++ ; for ( int i = 0 ; i < n && i < m ; i ++ ) { if ( a [ i ] == ' T ' ) answer += count_of_truck_in_lane_b ; if ( b [ i ] == ' T ' ) count_of_truck_in_lane_b -- ; } return answer ; } static void findProbability ( String a , String b ) { int total_outcome = count_of_accident ( a , b ) ; double favourable_outcome = count_of_collision ( a , b ) ; Console . Write ( " { 0 : F4 } " , favourable_outcome / total_outcome ) ; } public static void Main ( String [ ] args ) { String S = " TCCBCTTB " , T = " BTCCBBTT " ; findProbability ( S , T ) ; } }
Count alphanumeric palindromes of length N | C # program for the above approach ; Function to calculate ( x ^ y ) mod p ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Return the final result ; Driver Code ; Given N ; Base Case ; Check whether n is even or odd ; Function Call ; Function Call
using System ; class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } public static void Main ( ) { int N = 3 ; int flag , k , m = 0 ; if ( ( N == 1 ) || ( N == 2 ) ) Console . Write ( 62 ) ; else m = 1000000000 + 7 ; if ( N % 2 == 0 ) { k = N / 2 ; flag = 1 ; } else { k = ( N - 1 ) / 2 ; flag = 0 ; } if ( flag != 0 ) { int a = power ( 62 , k , m ) ; Console . Write ( a ) ; } else { int a = power ( 62 , ( k + 1 ) , m ) ; Console . Write ( a ) ; } } }
Length of the longest substring with every character appearing even number of times | C # program for the above approach ; Function to find length of the longest subString with each element occurring even number of times ; Initialize unordered_map ; Stores the length of the longest required subString ; Traverse the String ; Stores the value of the digit present at current index ; Bitwise XOR of the mask with 1 left - shifted by val ; Check if the value of mask is already present in ind or not ; Update the readonly answer ; Otherwise ; Return the answer ; Driver Code ; Given String ; Length of the given String ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int lenOflongestReqSubstr ( String s , int N ) { Dictionary < int , int > ind = new Dictionary < int , int > ( ) ; int mask = 0 ; ind . Add ( 0 , - 1 ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int val = s [ i ] - '0' ; mask ^= ( 1 << val ) ; if ( ind . ContainsKey ( mask ) ) { ans = Math . Max ( ans , i - ind [ mask ] ) ; } else ind . Add ( mask , i ) ; } return ans ; } public static void Main ( String [ ] args ) { String s = "223015150" ; int N = s . Length ; Console . Write ( lenOflongestReqSubstr ( s , N ) ) ; } }
Count distinct strings possible by replacing each character by its Morse code | C # program to implement the above approach ; Function to count unique array elements by replacing each character by its Morse code ; Stores Morse code of all lowercase characters ; Stores distinct elements of String by replacing each character by Morse code ; Stores length of [ ] arr array ; Traverse the array ; Stores the Morse code of arr [ i ] ; Stores length of current String ; Update temp ; Insert temp into st ; Return count of elements in the set ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int uniqueMorseRep ( String [ ] arr ) { String [ ] morseCode = { " . - " , " - . . . " , " - . - . " , " - . . " , " . " , " . . - . " , " - - . " , " . . . . " , " . . " , " . - - - " , " - . - " , " . - . . " , " - - " , " - . " , " - - - " , " . - - . " , " - - . - " , " . - . " , " . . . " , " - " , " . . - " , " . . . - " , " . - - " , " - . . - " , " - . - - " , " - - . . " } ; HashSet < String > st = new HashSet < String > ( ) ; int N = arr . Length ; for ( int i = 0 ; i < N ; i ++ ) { String temp = " " ; int M = arr [ i ] . Length ; for ( int j = 0 ; j < M ; j ++ ) { temp += morseCode [ arr [ i ] [ j ] - ' a ' ] ; } st . Add ( temp ) ; } return st . Count ; } public static void Main ( String [ ] args ) { String [ ] arr = { " gig " , " zeg " , " gin " , " msn " } ; Console . Write ( uniqueMorseRep ( arr ) + " STRNEWLINE " ) ; } }
Maximum repeating character for every index in given String | C # program to implement the above approach ; Function to print the maximum repeating character at each index of the string ; Stores frequency of each distinct character ; Stores frequency of maximum repeating character ; Stores the character having maximum frequency ; Traverse the string ; Stores current character ; Update the frequency of str [ i ] ; If frequency of current character exceeds max ; Update max ; Update charMax ; Print the required output ; Driver Code ; Stores length of str
using System ; class GFG { public static void findFreq ( String str , int N ) { int [ ] freq = new int [ 256 ] ; int max = 0 ; char charMax = '0' ; for ( int i = 0 ; i < N ; i ++ ) { char ch = str [ i ] ; freq [ ch ] ++ ; if ( freq [ ch ] >= max ) { max = freq [ ch ] ; charMax = ch ; } Console . WriteLine ( charMax + " ▁ - > ▁ " + max ) ; } } public static void Main ( String [ ] args ) { String str = " abbc " ; int N = str . Length ; findFreq ( str , N ) ; } }
Minimize cost to convert given string to a palindrome | C # program for the above approach ; Function to find the minimum cost to convert the string into a palindrome ; Length of the string ; If iointer is in the second half ; Reverse the string ; Pointing to initial position ; Find the farthest index needed to change on left side ; Find the farthest index needed to change on right side ; Changing the variable to make string palindrome ; min distance to travel to make string palindrome ; Total cost ; Return the minimum cost ; Driver Code ; Given string S ; Given pointer P ; Function Call
using System ; class GFG { static int findMinCost ( string str , int pos ) { int n = str . Length ; char [ ] charArray = str . ToCharArray ( ) ; if ( pos >= n / 2 ) { Array . Reverse ( charArray ) ; pos = n - pos - 1 ; } int left , right ; left = right = pos ; for ( int i = pos ; i >= 0 ; -- i ) { if ( charArray [ i ] != charArray [ n - i - 1 ] ) { left = i ; } } for ( int i = pos ; i < n / 2 ; ++ i ) { if ( charArray [ i ] != charArray [ n - i - 1 ] ) { right = i ; } } int ans = 0 ; for ( int i = left ; i <= right ; ++ i ) { if ( charArray [ i ] != charArray [ n - i - 1 ] ) ans += 1 ; } int dis = Math . Min ( ( 2 * ( pos - left ) + ( right - pos ) ) , ( 2 * ( right - pos ) + ( pos - left ) ) ) ; ans = ans + dis ; return ans ; } public static void Main ( ) { string S = " bass " ; int P = 3 ; Console . Write ( findMinCost ( S , P ) ) ; } }
Minimize replacements by previous or next alphabet required to make all characters of a string the same | C # program for the above approach ; Function to find the minimum count of operations to make all characters of the String same ; Set min to some large value ; Find minimum operations for each character ; Initialize cnt ; Add the value to cnt ; Update minValue ; Return minValue ; Driver code ; Given String str ; Function call
using System ; class GFG { static int minCost ( string s , int n ) { int minValue = 100000000 ; for ( int i = 0 ; i <= 25 ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < n ; j ++ ) { cnt += Math . Min ( Math . Abs ( i - ( s [ j ] - ' a ' ) ) , 26 - Math . Abs ( i - ( s [ j ] - ' a ' ) ) ) ; } minValue = Math . Min ( minValue , cnt ) ; } return minValue ; } public static void Main ( ) { string str = " geeksforgeeks " ; int N = str . Length ; Console . WriteLine ( minCost ( str , N ) ) ; } }