codeparrot/xlcost-text-to-code huggingface dataset:
Minimum operations required to make the string satisfy the given condition | C # implementation of the approach ; Function to return the minimum operations required ; To store the first and the last occurrence of all the characters ; Set the first and the last occurrence of all the characters to - 1 ; Update the occurrences of the characters ; Only set the first occurrence if it hasn 't already been set ; To store the minimum operations ; If the frequency of the current character in the string is less than 2 ; Count of characters to be removed so that the string starts and ends at the current character ; Driver code
using System ; class GFG { readonly static int MAX = 26 ; static int minOperation ( String str , int len ) { int [ ] first = new int [ MAX ] ; int [ ] last = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( int i = 0 ; i < len ; i ++ ) { int index = ( str [ i ] - ' a ' ) ; if ( first [ index ] == - 1 ) first [ index ] = i ; last [ index ] = i ; } int minOp = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( first [ i ] == - 1 first [ i ] == last [ i ] ) continue ; int cnt = len - ( last [ i ] - first [ i ] + 1 ) ; if ( minOp == - 1 cnt < minOp ) minOp = cnt ; } return minOp ; } public static void Main ( String [ ] args ) { String str = " abcda " ; int len = str . Length ; Console . WriteLine ( minOperation ( str , len ) ) ; } }
Queries to find the count of vowels in the substrings of the given string | C # implementation of the approach ; Function that returns true if ch is a vowel ; Function to return the count of vowels in the substring str [ l ... r ] ; To store the count of vowels ; For every character in the index range [ l , r ] ; If the current character is a vowel ; For every query ; Find the count of vowels for the current query ; Driver code
using System ; class GFG { static int N = 2 ; static Boolean isVowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } static int countVowels ( String str , int l , int r ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( isVowel ( str [ i ] ) ) cnt ++ ; } return cnt ; } static void performQueries ( String str , int [ , ] queries , int q ) { for ( int i = 0 ; i < q ; i ++ ) { Console . WriteLine ( countVowels ( str , queries [ i , 0 ] , queries [ i , 1 ] ) ) ; } } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int [ , ] queries = { { 1 , 3 } , { 2 , 4 } , { 1 , 9 } } ; int q = queries . GetLength ( 0 ) ; performQueries ( str , queries , q ) ; } }
Convert a String to a Singly Linked List | C # program to Convert a String to a Singly Linked List ; Structure for a Singly Linked List ; Function to add a new node to the Linked List ; Function to convert the string to Linked List . ; curr pointer points to the current node where the insertion should take place ; Function to print the data present in all the nodes ; Driver code
using System ; class GFG { class node { public char data ; public node next ; } ; static node add ( char data ) { node newnode = new node ( ) ; newnode . data = data ; newnode . next = null ; return newnode ; } static node string_to_SLL ( String text , node head ) { head = add ( text [ 0 ] ) ; node curr = head ; for ( int i = 1 ; i < text . Length ; i ++ ) { curr . next = add ( text [ i ] ) ; curr = curr . next ; } return head ; } static void print ( node head ) { node curr = head ; while ( curr != null ) { Console . Write ( curr . data + " ▁ - > ▁ " ) ; curr = curr . next ; } } public static void Main ( String [ ] args ) { String text = " GEEKS " ; node head = null ; head = string_to_SLL ( text , head ) ; print ( head ) ; } }
Reduce the string to minimum length with the given operation | C # implementation of the approach ; Function to return the minimum possible length str can be reduced to with the given operation ; Stack to store the characters of the given string ; For every character of the string ; If the stack is empty then push the current character in the stack ; Get the top character ; If the top element is not equal to the current element and it only differs in the case ; Pop the top element from stack ; Else push the current element ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minLength ( String str , int len ) { Stack < char > s = new Stack < char > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( s . Count == 0 ) { s . Push ( str [ i ] ) ; } else { char c = s . Peek ( ) ; if ( c != str [ i ] && char . ToUpper ( c ) == char . ToUpper ( ( str [ i ] ) ) ) { s . Pop ( ) ; } else { s . Push ( str [ i ] ) ; } } } return s . Count ; } public static void Main ( String [ ] args ) { String str = " ASbBsd " ; int len = str . Length ; Console . WriteLine ( minLength ( str , len ) ) ; } }
Check whether two strings can be made equal by copying their characters with the adjacent ones | C # implementation of the above approach ; Function that returns true if both the strings can be made equal with the given operation ; Lengths of both the strings have to be equal ; To store the frequency of the characters of str1 ; For every character of str2 ; If current character of str2 also appears in str1 ; Driver code
using System ; class GFG { static int MAX = 26 ; static Boolean canBeMadeEqual ( String str1 , String str2 ) { int len1 = str1 . Length ; int len2 = str2 . Length ; if ( len1 == len2 ) { int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < len1 ; i ++ ) { freq [ str1 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < len2 ; i ++ ) { if ( freq [ str2 [ i ] - ' a ' ] > 0 ) return true ; } } return false ; } public static void Main ( String [ ] args ) { String str1 = " abc " , str2 = " defa " ; if ( canBeMadeEqual ( str1 , str2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Minimum characters that are to be inserted such that no three consecutive characters are same | C # implementation of the above approach ; Function to return the count of characters that are to be inserted in str such that no three consecutive characters are same ; To store the count of operations required ; A character needs to be inserted after str [ i + 1 ] ; Current three consecutive characters are not same ; Driver code
using System ; class GFG { static int getCount ( string str , int n ) { int cnt = 0 ; int i = 0 ; while ( i < n - 2 ) { if ( str [ i ] == str [ i + 1 ] && str [ i ] == str [ i + 2 ] ) { cnt ++ ; i = i + 2 ; } else { i ++ ; } } return cnt ; } static public void Main ( ) { string str = " aabbbcc " ; int n = str . Length ; Console . WriteLine ( getCount ( str , n ) ) ; } }
Find the number of strings formed using distinct characters of a given string | C # implementation of the approach ; Function to return the factorial of n ; Function to return the count of all possible strings that can be formed with the characters of the given string without repeating characters ; To store the distinct characters of the string str ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int fact ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact *= i ; return fact ; } static int countStrings ( String str , int n ) { HashSet < char > distinct_char = new HashSet < char > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { distinct_char . Add ( str [ i ] ) ; } return fact ( distinct_char . Count ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . Length ; Console . WriteLine ( countStrings ( str , n ) ) ; } }
Find the character made by adding all the characters of the given string | C # implementation of the approach ; Function to return the required character ; To store the sum of the characters of the given string ; Add the current character to the sum ; Return the required character ; Driver code
using System ; class GFG { static char getChar ( String str ) { int sum = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { sum += ( str [ i ] - ' a ' + 1 ) ; } if ( sum % 26 == 0 ) return ' z ' ; else { sum = sum % 26 ; return ( char ) ( ' a ' + sum - 1 ) ; } } public static void Main ( String [ ] args ) { String str = " gfg " ; Console . WriteLine ( getChar ( str ) ) ; } }
Reverse the given string in the range [ L , R ] | C # implementation of the approach ; Function to return the string after reversing characters in the range [ L , R ] ; Invalid range ; While there are characters to swap ; Swap ( str [ l ] , str [ r ] ) ; Driver code
using System ; class GFG { static String reverse ( char [ ] str , int len , int l , int r ) { if ( l < 0 r >= len l > r ) return " Invalid ▁ range ! " ; while ( l < r ) { char c = str [ l ] ; str [ l ] = str [ r ] ; str [ r ] = c ; l ++ ; r -- ; } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . Length ; int l = 5 , r = 7 ; Console . WriteLine ( reverse ( str . ToCharArray ( ) , len , l , r ) ) ; } }
Program to Encrypt a String using ! and @ | C # program to Encrypt the String using ! and @ ; Function to encrypt the string ; evenPos is for storing encrypting char at evenPosition oddPos is for storing encrypting char at oddPosition ; Get the number of times the character is to be repeated ; if i is odd , print ' ! ' else print ' @ ' ; Driver code ; Encrypt the String
using System ; using System . Collections . Generic ; class GFG { static void encrypt ( char [ ] input ) { char evenPos = ' @ ' , oddPos = ' ! ' ; int repeat , ascii ; for ( int i = 0 ; i < input . Length ; i ++ ) { ascii = input [ i ] ; repeat = ascii >= 97 ? ascii - 96 : ascii - 64 ; for ( int j = 0 ; j < repeat ; j ++ ) { if ( i % 2 == 0 ) Console . Write ( " { 0 } " , oddPos ) ; else Console . Write ( " { 0 } " , evenPos ) ; } } } public static void Main ( String [ ] args ) { char [ ] input = { ' A ' , ' b ' , ' C ' , ' d ' } ; encrypt ( input ) ; } }
Check if expression contains redundant bracket or not | Set 2 | C # program to check for redundant braces in the string ; Function to check for redundant braces ; count of no of signs ; Driver Code
using System ; class GFG { static bool IsRedundantBraces ( string A ) { int a = 0 , b = 0 ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( A [ i ] == ' ( ' && A [ i + 2 ] == ' ) ' ) return true ; if ( A [ i ] == ' * ' A [ i ] == ' + ' A [ i ] == ' - ' A [ i ] == ' / ' ) a ++ ; if ( A [ i ] == ' ( ' ) b ++ ; } if ( b > a ) return true ; return false ; } public static void Main ( String [ ] args ) { String A = " ( ( ( a + b ) ▁ + ▁ c ) ▁ + ▁ d ) " ; if ( IsRedundantBraces ( A ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
Convert an unbalanced bracket sequence to a balanced sequence | C # implementation of the approach ; Function to return balancedBrackets string ; Initializing dep to 0 ; Stores maximum negative depth ; if dep is less than minDep ; if minDep is less than 0 then there is need to add ' ( ' at the front ; Reinitializing to check the updated string ; if dep is not 0 then there is need to add ' ) ' at the back ; Driver code
using System ; class GFG { static string balancedBrackets ( string str ) { int dep = 0 ; int minDep = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' ( ' ) dep ++ ; else dep -- ; if ( minDep > dep ) minDep = dep ; } if ( minDep < 0 ) { for ( int i = 0 ; i < Math . Abs ( minDep ) ; i ++ ) str = ' ( ' + str ; } dep = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' ( ' ) dep ++ ; else dep -- ; } if ( dep != 0 ) { for ( int i = 0 ; i < dep ; i ++ ) str = str + ' ) ' ; } return str ; } public static void Main ( ) { String str = " ) ) ) ( ) " ; Console . WriteLine ( balancedBrackets ( str ) ) ; } }
Minimum operations required to convert a binary string to all 0 s or all 1 s | C # implementation of the approach ; Function to return the count of minimum operations required ; Increment count when consecutive characters are different ; Answer is rounding off the ( count / 2 ) to lower ; Driver code
using System ; class GFG { static int minOperations ( string str , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ ( i ) ] != str [ ( i + 1 ) ] ) count ++ ; } return ( count + 1 ) / 2 ; } public static void Main ( ) { string str = "000111" ; int n = str . Length ; Console . WriteLine ( minOperations ( str , n ) ) ; } }
Append a digit in the end to make the number equal to the length of the remaining string | C # implementation of the approach ; Function to return the required digit ; To store the position of the first numeric digit in the string ; To store the length of the string without the numeric digits in the end ; pw stores the current power of 10 and num is to store the number which is appended in the end ; If current character is a numeric digit ; Get the current digit ; Build the number ; If number exceeds the length ; Next power of 10 ; Append 0 in the end ; Required number that must be added ; If number is not a single digit ; Driver code
using System ; class GFG { static int find_digit ( String s , int n ) { int first_digit = - 1 , i ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] < '0' s [ i ] > '9' ) { first_digit = i ; break ; } } first_digit ++ ; int s_len = first_digit ; int num = 0 , pw = 1 ; i = n - 1 ; while ( i >= 0 ) { if ( s [ i ] >= '0' && s [ i ] <= '9' ) { int digit = s [ i ] - '0' ; num = num + ( pw * digit ) ; if ( num >= s_len ) return - 1 ; pw = pw * 10 ; } i -- ; } num = num * 10 ; int req = s_len - num ; if ( req > 9 req < 0 ) return - 1 ; return req ; } public static void Main ( String [ ] args ) { String s = " abcd0" ; int n = s . Length ; Console . Write ( find_digit ( s , n ) ) ; } }
Check whether str1 can be converted to str2 with the given operations | C # implementation of the approach ; Function that returns true if str1 can be converted to str2 with the given operations ; Traverse from left to right ; If the two characters do not match ; If possible to combine ; If not possible to combine ; If the two characters match ; If possible to convert one string to another ; Driver code
using System ; class GFG { static bool canConvert ( string str1 , string str2 ) { int i = 0 , j = 0 ; while ( i < str1 . Length && j < str2 . Length ) { if ( str1 [ i ] != str2 [ j ] ) { if ( str1 [ i ] == '0' && str2 [ j ] == '1' && i + 1 < str1 . Length && str1 [ i + 1 ] == '0' ) { i += 2 ; j ++ ; } else { return false ; } } else { i ++ ; j ++ ; } } if ( i == str1 . Length && j == str2 . Length ) return true ; return false ; } public static void Main ( ) { string str1 = "00100" , str2 = "111" ; if ( canConvert ( str1 , str2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Reverse the Words of a String using Stack | C # implementation of the above approach ; Function to reverse the words of the given String without using strtok ( ) . ; Create an empty String stack ; Create an empty temporary String ; Traversing the entire String ; Push the temporary variable into the stack ; Assigning temporary variable as empty ; For the last word of the String ; Get the words in reverse order ; Driver code
using System ; using System . Collections ; class GFG { static void reverse ( string s ) { Stack stc = new Stack ( ) ; string temp = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' ▁ ' ) { stc . Push ( temp ) ; temp = " " ; } else { temp = temp + s [ i ] ; } } stc . Push ( temp ) ; while ( stc . Count != 0 ) { temp = ( string ) stc . Peek ( ) ; Console . Write ( temp + " ▁ " ) ; stc . Pop ( ) ; } Console . WriteLine ( ) ; } static void Main ( ) { string s = " I ▁ Love ▁ To ▁ Code " ; reverse ( s ) ; } }
Print an N x M matrix such that each row and column has all the vowels in it | C # implementation of the approach ; Function to print the required matrix ; Impossible to generate the required matrix ; Store all the vowels ; Print the matrix ; Print vowels for every index ; Shift the vowels by one ; Driver code
using System ; class GFG { static void printMatrix ( int n , int m ) { if ( n < 5 m < 5 ) { Console . Write ( - 1 ) ; return ; } char [ ] s = " aeiou " . ToCharArray ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { Console . Write ( s [ j % 5 ] + " ▁ " ) ; } Console . WriteLine ( ) ; char c = s [ 0 ] ; for ( int k = 0 ; k < 4 ; k ++ ) { s [ k ] = s [ k + 1 ] ; } s [ 4 ] = c ; } } public static void Main ( String [ ] args ) { int n = 5 , m = 5 ; printMatrix ( n , m ) ; } }
Check if a given string is made up of two alternating characters | C # implementation of the approach ; Function that returns true if the string is made up of two alternating characters ; Check if ith character matches with the character at index ( i + 2 ) ; If string consists of a single character repeating itself ; Driver code
using System ; class GFG { static bool isTwoAlter ( string s ) { for ( int i = 0 ; i < s . Length - 2 ; i ++ ) { if ( s [ i ] != s [ i + 2 ] ) { return false ; } } if ( s [ 0 ] == s [ 1 ] ) return false ; return true ; } public static void Main ( ) { string str = " ABAB " ; if ( isTwoAlter ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Number of character corrections in the given strings to make them equal | C # implementation of the approach ; Function to return the count of operations required ; To store the count of operations ; No operation required ; One operation is required when any two characters are equal ; Two operations are required when none of the characters are equal ; Return the minimum count of operations required ; Driver code
using System ; class GFG { static int minOperations ( int n , string a , string b , string c ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char x = a [ i ] ; char y = b [ i ] ; char z = c [ i ] ; if ( x == y && y == z ) { ; } else if ( x == y y == z x == z ) { ans ++ ; } else { ans += 2 ; } } return ans ; } public static void Main ( ) { string a = " place " ; string b = " abcde " ; string c = " plybe " ; int n = a . Length ; Console . Write ( minOperations ( n , a , b , c ) ) ; } }
Check if string can be made lexicographically smaller by reversing any substring | C # implementation of the approach ; Function that returns true if s can be made lexicographically smaller by reversing a sub - string in s ; Traverse in the string ; Check if s [ i + 1 ] < s [ i ] ; Not possible ; Driver code
using System ; class GFG { static bool check ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) return true ; } return false ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; if ( check ( s ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count of sub | C # implementation of the approach ; Function to return the count of required sub - strings ; Number of sub - strings from position of current x to the end of str ; To store the number of characters before x ; Driver code
using System ; class GFG { static int countSubStr ( String str , int n , char x ) { int res = 0 , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == x ) { res += ( ( count + 1 ) * ( n - i ) ) ; count = 0 ; } else count ++ ; } return res ; } public static void Main ( String [ ] args ) { String str = " abcabc " ; int n = str . Length ; char x = ' c ' ; Console . WriteLine ( countSubStr ( str , n , x ) ) ; } }
Count of sub | C # implementation of the approach ; Function to return the count of possible sub - strings of length n ; Driver code
using System ; class GFG { static int countSubStr ( string str , int n ) { int len = str . Length ; return ( len - n + 1 ) ; } public static void Main ( ) { string str = " geeksforgeeks " ; int n = 5 ; Console . WriteLine ( countSubStr ( str , n ) ) ; } }
Count of sub | C # implementation of the approach ; Function to return the number of sub - strings that do not contain the given character c ; Length of the string ; Traverse in the string ; If current character is different from the given character ; Update the number of sub - strings ; Reset count to 0 ; For the characters appearing after the last occurrence of c ; Driver code
using System ; class GFG { static int countSubstrings ( string s , char c ) { int n = s . Length ; int cnt = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] != c ) cnt ++ ; else { sum += ( cnt * ( cnt + 1 ) ) / 2 ; cnt = 0 ; } } sum += ( cnt * ( cnt + 1 ) ) / 2 ; return sum ; } public static void Main ( ) { string s = " baa " ; char c = ' b ' ; Console . Write ( countSubstrings ( s , c ) ) ; } }
Minimize ASCII values sum after removing all occurrences of one character | C # implementation of the approach ; Function to return the minimized sum ; To store the occurrences of each character of the string ; Update the occurrence ; Calculate the sum ; Get the character which is contributing the maximum value to the sum ; Count of occurrence of the character multiplied by its ASCII value ; Return the minimized sum ; Driver code
using System ; class GfG { static int getMinimizedSum ( string str , int len ) { int i , maxVal = Int32 . MinValue , sum = 0 ; int [ ] occurrences = new int [ 26 ] ; for ( i = 0 ; i < len ; i ++ ) { occurrences [ str [ i ] - ' a ' ] ++ ; sum += ( int ) str [ i ] ; } for ( i = 0 ; i < 26 ; i ++ ) maxVal = Math . Max ( maxVal , occurrences [ i ] * ( i + ' a ' ) ) ; return ( sum - maxVal ) ; } public static void Main ( ) { string str = " geeksforgeeks " ; int len = str . Length ; Console . WriteLine ( getMinimizedSum ( str , len ) ) ; } }
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | C # implementation of the approach ; Function that returns true if s is palindrome ; Function to return the required index ; Copy the ith character in S ; Copy all the character of string s2 in Temp ; Check whether the string is palindrome ; Driver code
using System ; class GFG { static bool isPalindrome ( string s ) { int i = 0 ; int j = s . Length - 1 ; while ( i < j ) { if ( s [ i ] != s [ j ] ) return false ; i ++ ; j -- ; } return true ; } static int getIndex ( string S1 , string S2 , int n ) { string S = " " ; for ( int i = 0 ; i < n ; i ++ ) { S = S + S1 [ i ] ; string Temp = " " ; for ( int j = i + 1 ; j < n ; j ++ ) Temp += S2 [ j ] ; if ( isPalindrome ( S + Temp ) ) { return i ; } } return - 1 ; } public static void Main ( ) { string S1 = " abcdf " , S2 = " sfgba " ; int n = S1 . Length ; Console . WriteLine ( getIndex ( S1 , S2 , n ) ) ; } }
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | C # implementation of the above approach ; Function that returns true if the sub - String starting from index i and ending at index j is a palindrome ; Function to get the required index ; Start comparing the two Strings from both ends . ; Break from the loop at first mismatch ; If it is possible to concatenate the Strings to form palindrome , return index ; If remaining part for s2 is palindrome ; If remaining part for s1 is palindrome ; If not possible , return - 1 ; Driver Code
using System ; class GFG { static bool isPalindrome ( string s , int i , int j ) { while ( i < j ) { if ( s [ i ] != s [ j ] ) return false ; i ++ ; j -- ; } return true ; } static int getIndex ( string s1 , string s2 , int len ) { int i = 0 , j = len - 1 ; while ( i < j ) { if ( s1 [ i ] != s2 [ j ] ) { break ; } i ++ ; j -- ; } if ( i == j ) { return i - 1 ; } else if ( isPalindrome ( s2 , i , j ) ) return i - 1 ; else if ( isPalindrome ( s1 , i , j ) ) return j ; return - 1 ; } public static void Main ( ) { string s1 = " abcdf " , s2 = " sfgba " ; int len = s1 . Length ; Console . WriteLine ( getIndex ( s1 , s2 , len ) ) ; } }