codeparrot/xlcost-text-to-code huggingface dataset:
Generate a string consisting of characters ' a ' and ' b ' that satisfy the given conditions | C # implementation of the approach ; Function to generate and print the required string ; More ' b ' , append " bba " ; More ' a ' , append " aab " ; Equal number of ' a ' and ' b ' append " ab " ; Driver code
using System ; class GFG { static void generateString ( int A , int B ) { string rt = " " ; while ( 0 < A 0 < B ) { if ( A < B ) { if ( 0 < B -- ) { rt += ( ' b ' ) ; } if ( 0 < B -- ) { rt += ( ' b ' ) ; } if ( 0 < A -- ) { rt += ( ' a ' ) ; } } else if ( B < A ) { if ( 0 < A -- ) { rt += ( ' a ' ) ; } if ( 0 < A -- ) { rt += ( ' a ' ) ; } if ( 0 < B -- ) { rt += ( ' b ' ) ; } } else { if ( 0 < A -- ) { rt += ( ' a ' ) ; } if ( 0 < B -- ) { rt += ( ' b ' ) ; } } } Console . WriteLine ( rt ) ; } public static void Main ( ) { int A = 2 , B = 6 ; generateString ( A , B ) ; } }
Number of strings that satisfy the given condition | C # implementation of the approach ; Function to return the count of valid strings ; Set to store indices of valid strings ; Find the maximum digit for current position ; Add indices of all the strings in the set that contain maximal digit ; Return number of strings in the set ; Driver code
using System ; using System . Collections . Generic ; class GfG { static int countStrings ( int n , int m , String [ ] s ) { HashSet < int > ind = new HashSet < int > ( ) ; for ( int j = 0 ; j < m ; j ++ ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) mx = Math . Max ( mx , ( int ) ( s [ i ] [ j ] - '0' ) ) ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] [ j ] - '0' == mx ) ind . Add ( i ) ; } return ind . Count ; } public static void Main ( ) { String [ ] s = { "223" , "232" , "112" } ; int m = s [ 0 ] . Length ; int n = s . Length ; Console . WriteLine ( countStrings ( n , m , s ) ) ; } }
Lexicographically largest sub | C # implementation of the approach ; Function to return the lexicographically largest sub - sequence of s ; Get the max character from the String ; Use all the occurrences of the current maximum character ; Repeat the steps for the remaining String ; Driver code
using System ; class GFG { static String getSubSeq ( String s , int n ) { String res = " " ; int cr = 0 ; while ( cr < n ) { char mx = s [ cr ] ; for ( int i = cr + 1 ; i < n ; i ++ ) { mx = ( char ) Math . Max ( mx , s [ i ] ) ; } int lst = cr ; for ( int i = cr ; i < n ; i ++ ) { if ( s [ i ] == mx ) { res += s [ i ] ; lst = i ; } } cr = lst + 1 ; } return res ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . Length ; Console . WriteLine ( getSubSeq ( s , n ) ) ; } }
Minimum replacements such that the difference between the index of the same characters is divisible by 3 | C # program to find minimum replacements such that the difference between the index of the same characters is divisible by 3 ; Function to count the number of minimal replacements ; Generate all permutations ; Count the replacements ; Return the replacements ; Driver Code
using System ; class GFG { static int countMinimalReplacements ( String s ) { int n = s . Length ; int mini = int . MaxValue ; char [ ] dup = "012" . ToCharArray ( ) ; do { int dif = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] != dup [ i % 3 ] ) { dif ++ ; } } mini = Math . Min ( mini , dif ) ; } while ( next_permutation ( dup ) ) ; return mini ; } static bool next_permutation ( char [ ] p ) { for ( int a = p . Length - 2 ; a >= 0 ; -- a ) { if ( p [ a ] < p [ a + 1 ] ) { for ( int b = p . Length - 1 ; ; -- b ) { if ( p [ b ] > p [ a ] ) { char t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . Length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } } } } return false ; } public static void Main ( String [ ] args ) { String s = "2101200" ; Console . WriteLine ( countMinimalReplacements ( s ) ) ; } }
Count of sub | C # implementation of above approach ; Function to return the count of sub - strings of str that are divisible by k ; Take all sub - strings starting from i ; If current sub - string is divisible by k ; Return the required count ; Driver code
using System ; class GFG { static int countSubStr ( String str , int len , int k ) { int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int n = 0 ; for ( int j = i ; j < len ; j ++ ) { n = n * 10 + ( str [ j ] - '0' ) ; if ( n % k == 0 ) count ++ ; } } return count ; } public static void Main ( ) { String str = "33445" ; int len = str . Length ; int k = 11 ; Console . WriteLine ( countSubStr ( str , len , k ) ) ; } }
Find the resulting Colour Combination | C # program to find the resultant colour combination ; Function to return Colour Combination ; Check for B * G = Y ; Check for B * Y = G ; Check for Y * G = B ; Driver Code
using System ; class GFG { static char Colour_Combination ( string s ) { char temp = s [ 0 ] ; for ( int i = 1 ; i < s . Length ; i ++ ) { if ( temp != s [ i ] ) { if ( ( temp == ' B ' temp == ' G ' ) && ( s [ i ] == ' G ' s [ i ] == ' B ' ) ) temp = ' Y ' ; else if ( ( temp == ' B ' temp == ' Y ' ) && ( s [ i ] == ' Y ' s [ i ] == ' B ' ) ) temp = ' G ' ; else temp = ' B ' ; } } return temp ; } static void Main ( ) { string s = " GBYGB " ; Console . WriteLine ( Colour_Combination ( s ) ) ; } }
Reverse Middle X Characters | C # implementation of the above approach ; Function to reverse the middle x characters in a string ; Find the position from where the characters have to be reversed ; Print the first n characters ; Print the middle x characters in reverse ; Print the last n characters ; Drived code
using System ; class GFG { static void reverse ( string str , int x ) { int n = ( str . Length - x ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( str [ i ] ) ; for ( int i = n + x - 1 ; i >= n ; i -- ) Console . Write ( str [ i ] ) ; for ( int i = n + x ; i < str . Length ; i ++ ) Console . Write ( str [ i ] ) ; } public static void Main ( ) { string str = " geeksforgeeks " ; int x = 3 ; reverse ( str , x ) ; } }
Minimize the number of replacements to get a string with same number of ' a ' , ' b ' and ' c ' in it | C # program to minimize the number of replacements to get a string with same number of a , b and c in it ; Function to count numbers ; Count the number of ' a ' , ' b ' and ' c ' in string ; If equal previously ; If not a multiple of 3 ; Increase the number of a ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' b ' and ;c; ; Check if it is ' b ' and it more than n / 3 ; Check if it is ' c ' and it more than n / 3 ; Increase the number of b ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' c ' ; Check if it is ' c ' and it more than n / 3 ; Increase the number of c 's from back ; Check if it is ' a ' and it more than n / 3 ; Increase the number of b 's from back ; Check if it is ' a ' and it more than n / 3 ; Increase the number of c 's from back ; Check if it is ' b ' and it more than n / 3 ; Driver Code
using System ; using System . Text ; class GFG { static string lexoSmallest ( string str , int n ) { int ca = 0 , cb = 0 , cc = 0 ; StringBuilder s = new StringBuilder ( str ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( s [ j ] == ' a ' ) ca ++ ; else if ( s [ j ] == ' b ' ) cb ++ ; else cc ++ ; } if ( ca == cb && cb == cc ) { return s . ToString ( ) ; } int cnt = n / 3 ; if ( cnt * 3 != n ) { return " - 1" ; } int i = 0 ; while ( ca < cnt && i < n ) { if ( s [ i ] == ' b ' && cb > cnt ) { cb -- ; s [ i ] = ' a ' ; ca ++ ; } else if ( s [ i ] == ' c ' && cc > cnt ) { cc -- ; s [ i ] = ' a ' ; ca ++ ; } i ++ ; } i = 0 ; while ( cb < cnt && i < n ) { if ( s [ i ] == ' c ' && cc > cnt ) { cc -- ; s [ i ] = '1' ; cb ++ ; } i ++ ; } i = n - 1 ; while ( cc < cnt && i >= 0 ) { if ( s [ i ] == ' a ' && ca > cnt ) { ca -- ; s [ i ] = ' c ' ; cc ++ ; } i -- ; } i = n - 1 ; while ( cb < cnt && i >= 0 ) { if ( s [ i ] == ' a ' && ca > cnt ) { ca -- ; s [ i ] = ' b ' ; cb ++ ; } i -- ; } i = n - 1 ; while ( cc < cnt && i >= 0 ) { if ( s [ i ] == ' b ' && cb > cnt ) { cb -- ; s [ i ] = ' c ' ; cc ++ ; } i -- ; } return s . ToString ( ) ; } public static void Main ( string [ ] args ) { string s = " aaaaaa " ; int n = s . Length ; Console . Write ( lexoSmallest ( s , n ) ) ; } }
Minimum moves to reach from i to j in a cyclic string | C # implementation of the approach ; Function to return the count of steps required to move from i to j ; Starting from i + 1 ; Count of steps ; Current character ; If current character is different from previous ; Increment steps ; Update current character ; Return total steps ; Function to return the minimum number of steps required to reach j from i ; Swap the values so that i <= j ; Steps to go from i to j ( left to right ) ; While going from i to j ( right to left ) First go from i to 0 then from ( n - 1 ) to j ; If first and last character is different then it 'll add a step to stepsToLeft ; Return the minimum of two paths ; Driver code
using System ; class GFG { static int getSteps ( string str , int i , int j , int n ) { int k = i + 1 ; int steps = 0 ; char ch = str [ i ] ; while ( k <= j ) { if ( str [ k ] != ch ) { steps ++ ; ch = str [ k ] ; } k ++ ; } return steps ; } static int getMinSteps ( string str , int i , int j , int n ) { if ( j < i ) { int temp = i ; i = j ; j = temp ; } int stepsToRight = getSteps ( str , i , j , n ) ; int stepsToLeft = getSteps ( str , 0 , i , n ) + getSteps ( str , j , n - 1 , n ) ; if ( str [ 0 ] != str [ n - 1 ] ) stepsToLeft ++ ; return Math . Min ( stepsToLeft , stepsToRight ) ; } public static void Main ( ) { string str = " SSNSS " ; int n = str . Length ; int i = 0 , j = 3 ; Console . WriteLine ( getMinSteps ( str , i , j , n ) ) ; } }
Print all permutation of a string using ArrayList | C # implementation of the approach ; Utility function to print the contents of the ArrayList ; Function to returns the arraylist which contains all the permutation of str ; If string is empty ; Return an empty arraylist ; Take first character of str ; Take sub - string starting from the second character ; Recurvise call ; Store the generated permutations into the resultant arraylist ; Return the resultant arraylist ; Driver code
using System . Collections . Generic ; using System ; class GFG { static void printArrayList ( List < String > arrL ) { arrL . Remove ( " " ) ; for ( int i = 0 ; i < arrL . Count ; i ++ ) Console . Write ( arrL [ i ] + " ▁ " ) ; } public static List < String > getPermutation ( String str ) { if ( str . Length == 0 ) { List < String > empty = new List < String > ( ) ; empty . Add ( " " ) ; return empty ; } char ch = str [ 0 ] ; String subStr = str . Substring ( 1 ) ; List < String > prevResult = getPermutation ( subStr ) ; List < String > Res = new List < String > ( ) ; foreach ( String val in prevResult ) { for ( int i = 0 ; i <= val . Length ; i ++ ) { Res . Add ( val . Substring ( 0 , i ) + ch + val . Substring ( i ) ) ; } } return Res ; } public static void Main ( String [ ] args ) { String str = " abc " ; printArrayList ( getPermutation ( str ) ) ; } }
Count distinct substrings that contain some characters at most k times | C # implementation of the approach ; Function to return the count of valid sub - strings ; Store all characters of anotherStr in a direct index table for quick lookup . ; To store distinct output substrings ; Traverse through the given string and one by one generate substrings beginning from s [ i ] . ; One by one generate substrings ending with s [ j ] ; If character is illegal ; If current substring is valid ; If current substring is invalid , adding more characters would not help . ; Return the count of distinct sub - strings ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX_CHAR = 256 ; static int countSubStrings ( String s , String anotherStr , int k ) { bool [ ] illegal = new bool [ MAX_CHAR ] ; for ( int i = 0 ; i < anotherStr . Length ; i ++ ) { illegal [ anotherStr [ i ] ] = true ; } HashSet < String > us = new HashSet < String > ( ) ; for ( int i = 0 ; i < s . Length ; ++ i ) { String ss = " " ; int count = 0 ; for ( int j = i ; j < s . Length ; ++ j ) { if ( illegal [ s [ j ] ] ) { ++ count ; } ss = ss + s [ j ] ; if ( count <= k ) { us . Add ( ss ) ; } else { break ; } } } return us . Count ; } public static void Main ( ) { String str = " acbacbacaa " ; String anotherStr = " abcdefghijklmnopqrstuvwxyz " ; int k = 2 ; Console . WriteLine ( countSubStrings ( str , anotherStr , k ) ) ; } }
Count pairs of parentheses sequences such that parentheses are balanced | C # program to count the number of pairs of balanced parentheses ; Function to count the number of pairs ; Hashing function to count the opening and closing brackets ; Traverse for all bracket sequences ; Get the string ; Counts the opening and closing required ; Traverse in the string ; If it is a opening bracket ; Closing bracket ; If openings are there , then close it ; Else increase count of closing ; If requirements of openings are there and no closing ; If requirements of closing are there and no opening ; Perfect ; Divide by two since two perfect makes one pair ; Traverse in the open and find corresponding minimum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( string [ ] bracks , int num ) { Dictionary < int , int > open = new Dictionary < int , int > ( ) ; Dictionary < int , int > close = new Dictionary < int , int > ( ) ; int cnt = 0 ; for ( int i = 0 ; i < num ; i ++ ) { string s = bracks [ i ] ; int l = s . Length ; int op = 0 , cl = 0 ; for ( int j = 0 ; j < l ; j ++ ) { if ( s [ j ] == ' ( ' ) op ++ ; else { if ( op != 0 ) op -- ; else cl ++ ; } } if ( op != 0 && cl == 0 ) { if ( open . ContainsKey ( op ) ) { open [ op ] ++ ; } else { open [ op ] = 1 ; } } if ( cl != 0 && op == 0 ) { if ( close . ContainsKey ( cl ) ) { close [ cl ] ++ ; } else { close [ cl ] = 1 ; } } if ( op == 0 && cl == 0 ) cnt ++ ; } cnt /= 2 ; foreach ( KeyValuePair < int , int > it in open ) { cnt += Math . Min ( it . Value , close [ it . Value ] ) ; } return cnt ; } public static void Main ( string [ ] args ) { string [ ] bracks = { " ) ( ) ) " , " ) " , " ( ( " , " ( ( " , " ( " , " ) " , " ) " } ; int num = bracks . Length ; Console . Write ( countPairs ( bracks , num ) ) ; } }
Remove consecutive alphabets which are in same case | C # program to remove the consecutive characters from a string that are in same case ; Function to return the modified string ; Traverse through the remaining characters in the string ; If the current and the previous characters are not in the same case then take the character ; Driver code
using System ; class GFG { static string removeChars ( string s ) { string modifiedStr = " " ; modifiedStr += s [ 0 ] ; for ( int i = 1 ; i < s . Length ; i ++ ) { if ( char . IsUpper ( s [ i ] ) && char . IsLower ( s [ i - 1 ] ) || char . IsLower ( s [ i ] ) && char . IsUpper ( s [ i - 1 ] ) ) modifiedStr += s [ i ] ; } return modifiedStr ; } public static void Main ( ) { string s = " GeeksForGeeks " ; Console . Write ( removeChars ( s ) ) ; } }
Cost to make a string Panagram | C # program to find the cost to make a string Panagram ; Function to return the total cost required to make the string Pangram ; Mark all the alphabets that occurred in the string ; Calculate the total cost for the missing alphabets ; Driver Code
using System ; class GFG { static int pangramCost ( int [ ] arr , string str ) { int cost = 0 ; bool [ ] occurred = new bool [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) occurred [ i ] = false ; for ( int i = 0 ; i < str . Length ; i ++ ) occurred [ str [ i ] - ' a ' ] = true ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( occurred [ i ] == false ) cost += arr [ i ] ; } return cost ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 } ; string str = " abcdefghijklmopqrstuvwz " ; Console . WriteLine ( pangramCost ( arr , str ) ) ; } }
Recursive program to insert a star between pair of identical characters | Recursive C # program to insert * between two consecutive same characters . ; Function to insert * at desired position ; Append current character ; If we reached last character ; If next character is same , append ' * ' ; Driver code
using System ; class GFG { static String output = " " ; static void pairStar ( String input , int i ) { output = output + input [ i ] ; if ( i == input . Length - 1 ) return ; if ( input [ i ] == input [ i + 1 ] ) output = output + ' * ' ; pairStar ( input , i + 1 ) ; } public static void Main ( String [ ] args ) { String input = " geeks " ; pairStar ( input , 0 ) ; Console . WriteLine ( output ) ; } }
Maximum number of removals of given subsequence from a string | C # implementation of the approach ; Function to return max possible operation of the given type that can be performed on str ; Increment count of sub - sequence ' g ' ; Increment count of sub - sequence ' gk ' if ' g ' is available ; Increment count of sub - sequence ' gks ' if sub - sequence ' gk ' appeared previously ; Return the count of sub - sequence ' gks ' ; Driver code
using System ; public class GFG { static int maxOperations ( string str ) { int i , g , gk , gks ; i = g = gk = gks = 0 ; for ( i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' g ' ) { g ++ ; } else if ( str [ i ] == ' k ' ) { if ( g > 0 ) { g -- ; gk ++ ; } } else if ( str [ i ] == ' s ' ) { if ( gk > 0 ) { gk -- ; gks ++ ; } } } return gks ; } public static void Main ( ) { string a = " ggkssk " ; Console . WriteLine ( maxOperations ( a ) ) ; } }
Split a string in equal parts such that all parts are palindromes | C # implementation of the approach ; Function to return the frequency array for the given string ; Function to return the required count ; Add frequencies of the even appearing characters ; Count of the characters that appeared odd number of times ; If there are no characters with odd frequency ; If there are no characters with even frequency ; Only a single character with odd frequency ; More than 1 character with odd frequency string isn 't a palindrome ; All odd appearing characters can also contribute to the even length palindrome if one character is removed from the frequency leaving it as even ; If k palindromes are possible where k is the number of characters with odd frequency ; Current character can no longer be an element in a string other than the mid character ; If current character has odd frequency > 1 take two characters which can be used in any of the parts ; Update the frequency ; If not possible , then every character of the string will act as a separate palindrome ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int [ ] getFrequencies ( String str ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < str . Length ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } return freq ; } static int countMinParts ( String str ) { int n = str . Length ; int [ ] freq = getFrequencies ( str ) ; List < int > oddFreq = new List < int > ( ) ; List < int > evenFreq = new List < int > ( ) ; int i , sumEven = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; if ( freq [ i ] % 2 == 0 ) evenFreq . Add ( freq [ i ] ) ; else oddFreq . Add ( freq [ i ] ) ; } for ( i = 0 ; i < evenFreq . Count ; i ++ ) { sumEven += evenFreq [ i ] ; } if ( oddFreq . Count == 0 ) return 1 ; if ( sumEven == 0 ) { if ( oddFreq . Count == 1 ) return 1 ; return 0 ; } i = 0 ; while ( i < oddFreq . Count ) { if ( ( sumEven / 2 ) % oddFreq . Count == 0 ) return oddFreq . Count ; if ( oddFreq [ i ] == 1 ) { i ++ ; continue ; } sumEven += 2 ; oddFreq . Insert ( i , oddFreq [ i ] - 2 ) ; } return n ; } public static void Main ( String [ ] args ) { String s = " noonpeep " ; Console . WriteLine ( countMinParts ( s ) ) ; } }
Substring Reverse Pattern | C # program to print the required pattern ; Function to print the required pattern ; Print the unmodified string ; Reverse the string ; Replace the first and last character by ' * ' then second and second last character and so on until the string has characters remaining ; Driver Code
using System ; class GFG { static void printPattern ( char [ ] s , int n ) { Console . WriteLine ( s ) ; int i = 0 , j = n - 1 ; while ( i < j ) { char c = s [ i ] ; s [ i ] = s [ j ] ; s [ j ] = c ; i ++ ; j -- ; } i = 0 ; j = n - 1 ; while ( j - i > 1 ) { s [ i ] = s [ j ] = ' * ' ; Console . WriteLine ( s ) ; i ++ ; j -- ; } } public static void Main ( String [ ] args ) { char [ ] s = " geeks " . ToCharArray ( ) ; int n = s . Length ; printPattern ( s , n ) ; } }
Check if the string satisfies the given condition | C # implementation of the approach ; Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if c is a vowel ; Function that returns true if the count of vowels in word is prime ; If count of vowels is prime ; Driver code
using System ; class GFG { static Boolean prime ( int n ) { if ( n <= 1 ) { return false ; } if ( n <= 3 ) { return true ; } if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static Boolean isVowel ( char c ) { c = char . ToLower ( c ) ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) { return true ; } return false ; } static Boolean isValidString ( String word ) { int cnt = 0 ; for ( int i = 0 ; i < word . Length ; i ++ ) { if ( isVowel ( word [ i ] ) ) { cnt ++ ; } } if ( prime ( cnt ) ) { return true ; } else { return false ; } } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; if ( isValidString ( s ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Concatenate suffixes of a String | C # Implementation of the above approach ; Function to print the expansion of the string ; Take sub - string from i to n - 1 ; Print the sub - string ; Driver code
using System ; class GFG { static void printExpansion ( String str ) { String suff = " " ; for ( int i = str . Length - 1 ; i >= 0 ; i -- ) { suff = suff + str [ i ] ; Console . Write ( suff ) ; } } public static void Main ( String [ ] args ) { String str = " geeks " ; printExpansion ( str ) ; } }
Solve the Logical Expression given by string | C # program to solve the logical expression . ; Function to evaluate the logical expression ; traversing string from the end . ; for NOT operation ; for AND and OR operation ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static char logicalExpressionEvaluation ( String str ) { Stack < char > arr = new Stack < char > ( ) ; for ( int i = str . Length - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' [ ' ) { List < char > s = new List < char > ( ) ; while ( arr . Peek ( ) != ' ] ' ) { s . Add ( arr . Peek ( ) ) ; arr . Pop ( ) ; } arr . Pop ( ) ; if ( s . Count == 3 ) { arr . Push ( s [ 2 ] == '1' ? '0' : '1' ) ; } else if ( s . Count == 5 ) { int a = s [ 0 ] - 48 , b = s [ 4 ] - 48 , c ; if ( s [ 2 ] == ' & ' ) { c = a & b ; } else { c = a | b ; } arr . Push ( ( char ) ( c + 48 ) ) ; } } else { arr . Push ( str [ i ] ) ; } } return arr . Peek ( ) ; } public static void Main ( String [ ] args ) { String str = " [ [0 , & ,1 ] , | , [ ! ,1 ] ] " ; Console . WriteLine ( logicalExpressionEvaluation ( str ) ) ; } }
Find number of substrings of length k whose sum of ASCII value of characters is divisible by k | C # program to find number of substrings of length k whose sum of ASCII value of characters is divisible by k ; Finding length of string ; finding sum of ASCII value of first substring ; Using sliding window technique to find sum of ASCII value of rest of the substring ; checking if sum is divisible by k ; Driver code
using System ; public class GFG { static int count ( string s , int k ) { int n = s . Length ; int d = 0 , i ; int count = 0 ; for ( i = 0 ; i < n ; i ++ ) d += s [ i ] ; if ( d % k == 0 ) count += 1 ; for ( i = k ; i < n ; i ++ ) { int prev = s [ i - k ] ; d -= prev ; d += s [ i ] ; if ( d % k == 0 ) count += 1 ; } return count ; } public static void Main ( ) { string s = " bcgabc " ; int k = 3 ; int ans = count ( s , k ) ; Console . Write ( ans ) ; } }